Note that there are some explanatory texts on larger screens.

plurals
  1. POPython client-server script hangs until I press [enter]
    primarykey
    data
    text
    <p>I have a basic client-server script in Python using sockets. The server binds to a specific port and waits for a client connection. When a client connects, they are presented with a raw_input prompt that sends the entered commands to a subproc on the server and pipes the output back to the client. Sometimes when I execute commands from the client, the output will hang and not present me with the raw_input prompt until I press the [enter] key. At first I thought this might have been a buffer problem but it happens when I use commands with a small output, like 'clear' or 'ls', etc.</p> <p>The client code:</p> <pre><code>import os, sys import socket from base64 import * import time try: HOST = sys.argv[1] PORT = int(sys.argv[2]) except IndexError: print("You must specify a host IP address and port number!") print("usage: ./handler_client.py 192.168.1.4 4444") sys.exit() socksize = 4096 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: server.connect((HOST, PORT)) print("[+] Connection established!") print("[+] Type ':help' to view commands.") except: print("[!] Connection error!") sys.exit(2) while True: data = server.recv(socksize) cmd = raw_input(data) server.sendall(str(cmd)) server.close() </code></pre> <p>Server code:</p> <pre><code>import os,sys import socket import time from subprocess import Popen,PIPE,STDOUT,call HOST = '' PORT = 4444 socksize = 4096 activePID = [] conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.bind((HOST, PORT)) conn.listen(5) print("Listening on TCP port %s" % PORT) def reaper(): while activePID: pid,stat = os.waitpid(0, os.WNOHANG) if not pid: break activePID.remove(pid) def handler(connection): time.sleep(3) while True: cmd = connection.recv(socksize) proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, ) stdout, stderr = proc.communicate() if cmd == ":killme": connection.close() sys.exit(0) elif proc: connection.send( stdout ) connection.send("\nshell =&gt; ") connection.close() os._exit(0) def accept(): while 1: global connection connection, address = conn.accept() print "[!] New connection!" connection.send("\nshell =&gt; ") reaper() childPid = os.fork() # forks the incoming connection and sends to conn handler if childPid == 0: handler(connection) else: activePID.append(childPid) accept() </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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