Python python difference noted on shell and code in file

I am trying to create a python client to communicate with my C server. Here's the code for the client:

import socket
s = socket.socket()
s.connect(("127.0.0.1", 12209))
print "preparing to send"
s.send("2")
s.send("mmm2.com")
s.send("mypwd")
s.send("5120")
print "Sent data"
root = s.recv(256)
print root

      

When I run this code in an interactive shell (GUI IDLE), of course line by line, everything works very well. But when I save this code in a file and try to run it, it freezes and stops responding as per windows, what am I just not doing?

+3


source to share


1 answer


If you enter line by line, the lines sent will most likely be received by the server one by one in separate calls recv()

.

When you execute it in a script, all calls send()

are fired right after each other without delay, and the server will probably receive all the data in one volume in one call recv()

. So the server will see "2mmm2.commypwd5120" and may not handle it correctly. He can expect more requests from the client.



You will need some explicit separation between the values, such as newlines, for the server to parse the received data correctly.

+2


source







All Articles