accepting socket in function, use conn. outside function

This is the place for queries that don't fit in any of the other categories.

accepting socket in function, use conn. outside function

Postby devidation » Thu May 08, 2014 2:09 pm

Hello, for a project i have to make a connection between 2 raspberry pi's.
I want to use a function for connecting, reading and writing.
However I would like to know something I cannot figure out. If I accept a connection like this: client, address = socket.accept() inside a function, how can i make this client useable outside the function.
I cannot just return the client through the function because i will call the function from a html page by using webiopi.
Thanks in advance.
Last edited by stranac on Thu May 08, 2014 2:33 pm, edited 1 time in total.
Reason: First post lock.
devidation
 
Posts: 6
Joined: Thu May 08, 2014 2:03 pm

Re: accepting socket in function, use conn. outside function

Postby lorddarthvader » Thu May 08, 2014 2:36 pm

make it global
Code: Select all
>>> s = ''
>>> def spam():
...  global s
...  s = 'egg'
...
>>> s
''
>>> spam()
>>> s
'egg'
>>>
--If at first you don't succeed, destroy all evidence that you tried.
My github
fite me
User avatar
lorddarthvader
 
Posts: 32
Joined: Mon Apr 07, 2014 11:38 am

Re: accepting socket in function, use conn. outside function

Postby devidation » Thu May 08, 2014 2:55 pm

lorddarthvader wrote:make it global
Code: Select all
>>> s = ''
>>> def spam():
...  global s
...  s = 'egg'
...
>>> s
''
>>> spam()
>>> s
'egg'
>>>

Thank you for your answer, however how do i define that variable conn? In C# you could write something like TCPClient client = new TCPClient()
In python though i'm not sure how I should define the variable "conn" outside the function as I cannot just do something like "conn = None" or Can I?
Thanks again.
devidation
 
Posts: 6
Joined: Thu May 08, 2014 2:03 pm

Re: accepting socket in function, use conn. outside function

Postby lorddarthvader » Thu May 08, 2014 2:57 pm

my mistake, you don't have to declare the variable outside the function
Code: Select all
>>> def s():
...  global k
...  k = 'hi'
...
>>> k
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'k' is not defined
>>> s()
>>> k
'hi'
>>>
--If at first you don't succeed, destroy all evidence that you tried.
My github
fite me
User avatar
lorddarthvader
 
Posts: 32
Joined: Mon Apr 07, 2014 11:38 am

Re: accepting socket in function, use conn. outside function

Postby devidation » Thu May 08, 2014 3:00 pm

lorddarthvader wrote:my mistake, you don't have to declare the variable outside the function
Code: Select all
>>> def s():
...  global k
...  k = 'hi'
...
>>> k
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'k' is not defined
>>> s()
>>> k
'hi'
>>>

Ok thank you very much, I'll try this in the week-end.
devidation
 
Posts: 6
Joined: Thu May 08, 2014 2:03 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests