Lachlan Mather Lachlan Mather -4 years ago 91
Python Question

Python - Sockets behaving oddly?

I have a client / server application, and it is very basic. The client sends the server a message with information containing a file name, the server then creates this file in the server directory, the client will then proceed to send all the binary data from the file that the client chose to the server (and the server writes it into the newly formed file). The server then needs to run this file and send all output back to client. It was working fine, until I hit this dead end and it keeps looping. Here's a simplified version of the code on the server:

if data_received.startswith("COMMAND:run_file"): #client tells server to create new file
#create file
while True:
#Get binary data from client
#If data is blank break
#Write data to new file
#Close file
#Open up new file using subprocess
p = Popen("python " + data[17:], shell=True,stdin=PIPE,stdout=PIPE,stderr=STDOUT)
print #Print output

Now whenever this code is run, the server will receive the binary data of the client's file fine, but then it will just sit there... waiting for something, and it is not waiting for a new command from the client (I know this much). I am confused, so could someone spot the (possibly) very obvious mistake I've made somewhere?


I have found where it is getting stuck, and it is after


Now I have no clue why this is getting stuck.

Answer Source

If you use conn.recv(1024) it will wait until it receives data. And since you are in a while true loop, it will continuously try to receive data, if all the data is sent it doesn't receive anything (instead of 'blank data'). Therefore, your 'if data is blank' is never true.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download