0

I have a big question I couldn't get answer from the web about sockets in python. I'm making a simple client program (python) based on socket: Connecting to a server.

I would like to make a function that its purpose is just to try to connect to the server, otherwise the application will not work. Because I had trouble with "global" socket variable across the whole class, I decided to make a local socket variable inside my main and pass it through all functions.

I wanted to make sure that I understand it 100% : Should I return the socket from the function that's trying to connect to the server ( otherwise sleeps for 0.5 a second and tries again ) OR I don't need to return the socket at all and the socket variable itself will be updated ?

UPDATE

#will try to connect to the server 
def try_connecting_to_server(socket_to_server):
 connected = False
 while not connected:
 try:
 socket_to_server.connect((HOST, PORT)) # connect to the server
 connected = True
 except:
 print "couldn't connect to server, sleeping for 0.5 seconds"
 time.sleep(0.5)
 return socket_to_server
def main():
 # start the socket to the server
 socket_to_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # setup the socket for future use
try:
 socket_to_server = try_connecting_to_server(socket_to_server)
 handle_missions(socket_to_server) # pass the socket
except:
 print "encountered an error"
finally:
 socket_to_server.sendall(PROTOCOL_CLOSE_KEY)
 socket_to_server.close()
if __name__ == "__main__":
 main()
asked Feb 5, 2017 at 17:24
4
  • 1
    Showing the code you're asking about would make it much easier to help, don't you think? Commented Feb 5, 2017 at 17:26
  • @JonathonReinhart , I'm sorry, updating my post right now. Commented Feb 5, 2017 at 17:26
  • function which executes only one function is useless - you could use socket_to_server.connect() directly in main Commented Feb 5, 2017 at 17:30
  • @furas , what I mean is that the try_connecting_to_server function will be a loop until the client is connected . Commented Feb 5, 2017 at 17:34

1 Answer 1

1
def try_connecting_to_server(socket_to_server):
 connected = False
 while not connected:
 try:
 socket_to_server.connect((HOST, PORT)) # connect to the server
 connected = True
 except:
 print "couldn't connect to server, sleeping for 0.5 seconds"
 time.sleep(0.5)
 return socket_to_server

There is no reason for this function to return socket_to_server.

Since a socket object is mutable, any changes to it inside the function (e.g. connecting it to a server) are visible to the function which called it.

You can verify that by making this change in main():

returned_sock = try_connecting_to_server(socket_to_server)
if returned_sock is socket_to_server:
 print "They refer to the exact same object!"

See How do I pass a variable by reference?

answered Feb 5, 2017 at 17:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.