Python - server and client issues

I am trying to create a base server and client script. The idea is that the client can connect to the server and execute commands. A kind of SSH, but very simple. Heres my server code:

import sys, os, socket host = '' port = 50103 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) print("Server started on port: ", port) s.listen(1) while (1): conn, addr = s.accept() print 'New connection from ', addr try: while True: rc = conn.recv(2) pipe = os.popen(rc) rl = pipe.readlines() fl = conn.makefile('w', 0) fl.writelines(rl[:-1]) fl.close() except IOError: conn.close() 

And here is my client:

 import sys, socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = 'localhost' port = input('Port: ') s.connect((host, port)) while (1): cmd = raw_input('$ ') s.send(cmd) file = s.makefile('r', 0) sys.stdout.writelines(file.readlines()) file.close() 

Here is my problem. I start the server and then start the client on the same computer. I enter the port and connect. Then I get raw_input, which is "$". If I find a command like "ls", it just hangs on the client side. I need to exit the server for the client to get the ls output. By the way, I am running Ubuntu Linux. Not sure if this is important.

0
python connection client
source share
2 answers

When you create a file () on a socket and then use readlines () on it, it will continue until you reach the end of the file, which in the case of a socket is closed at the other end.

Using makefile () in this case does not make any sense to me, especially since you create and close it after each command. Just use send () and recv () at both ends.

You probably also want to have some kind of actual “protocol”, so the server tells the client “HERE TO ACCEPT RESPONSE” and “THIS IS THE END OF RESPONSE” so that the client knows. Otherwise, it becomes difficult to know when to stop waiting for an answer. :)

Update with an example that works:

server.py:

 import sys, os, socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 50500)) print("Server started") s.listen(1) while True: print "Accepting" conn, addr = s.accept() print 'New connection from ', addr while True: try: rc = conn.recv(1024) print "Command", rc if not rc.strip(): continue if rc.strip() == 'END': print "Close" conn.send("**END**") conn.close() break else: conn.send("This is the result of command %s\n" % rc) except Exception: conn.close() sys.exit() 

client.py

 import sys, os, socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('localhost', 50500)) while True: cmd = raw_input('$ ') s.send(cmd) result = s.recv(1024) print result if result == "**END**": print "Ending" break 
+2
source share

It’s good that you only connect to the client once, and on the server you close the socket after each read.

You should look at this example.

http://ilab.cs.byu.edu/python/socket/echoserver.html

You are doing a few things wrong.

+1
source share

All Articles