Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python socket difference noted on shell and code in file

I'm trying to build a python client to interact 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 on the interactive shell (the GUI IDLE) of course line by line, everything runs very fine. But when i save this code in a file and try to run it, it hangs and stops responding according to windows, what's it that I'm just not doing?

like image 348
Kimutai Avatar asked Dec 06 '25 02:12

Kimutai


1 Answers

If you type it line by line, the sent strings are likely received by the server one after another in separate recv() calls.

When you execute it in a script, all the send() calls run immediately after each other without delay and the server will probably receive all the data in one bulk in a single recv() call. So the server will see "2mmm2.commypwd5120", and maybe not handle that correctly. It might wait for more input from the client.

You will need some explicit separation between the values, for example newline characters, so that the server can parse the received data correctly.

like image 106
sth Avatar answered Dec 08 '25 18:12

sth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!