Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want your client to repeat what it's doing, just add a loop in there ;)</p> <pre><code>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = 'localhost' port = input('Port: ') s.connect((host, port)) while True: cmd = raw_input('$ ') s.send(cmd) file = s.makefile('r', 0) sys.stdout.writelines(file.readlines()) </code></pre> <p>Should probably be closer to what you want.</p> <p>Other comments:</p> <pre><code>s.listen(1) </code></pre> <p>This statement should probably be moved outside of the while loop. You only need to call <code>listen</code> once.</p> <pre><code>pipe = os.popen(rc) </code></pre> <p>os.popen has been deprecated, use the <a href="http://docs.python.org/library/subprocess.html" rel="nofollow" title="Python Subprocess Module">subprocess module</a> instead.</p> <pre><code>file = s.makefile('r', 0) </code></pre> <p>You're opening a file, yet you never close the file. You should probably add a <code>file.close()</code> after your <code>sys.stdout.writelines()</code> call.</p> <p><strong>EDIT:</strong> to answer below comment; done here due to length and formatting</p> <p>As it stands, you read from the socket once, and then immediately close it. Thus, when the client goes to send the next command, it sees that the server closed the socket and indicates an error.</p> <p>The solution is to change your server code so that it can handle receiving multiple commands. Note that this is solved by introducing another loop.</p> <p>You need to wrap</p> <pre><code>rc = conn.recv(2) pipe = os.popen(rc) rl = pipe.readlines() fl = conn.makefile('w', 0) fl.writelines(rl[:-1]) </code></pre> <p>in another <code>while True:</code> loop so that it repeats until the client disconnects, and then wrap that in a try-except block that catches the IOError that is thrown by conn.recv() when the client disconnects.</p> <p>the try-except block should look like</p> <pre><code>try: # the described above loop goes here except IOError: conn.close() # execution continues on... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload