| 17 | BUFSIZE = 1024 |
| 18 | |
| 19 | def main(): |
| 20 | if len(sys.argv) > 1: |
| 21 | port = int(sys.argv[1]) |
| 22 | else: |
| 23 | port = PORT |
| 24 | s = socket(AF_INET, SOCK_STREAM) |
| 25 | s.bind(('', port)) |
| 26 | s.listen(1) |
| 27 | while True: |
| 28 | conn, (remotehost, remoteport) = s.accept() |
| 29 | with conn: |
| 30 | print('connection from', remotehost, remoteport) |
| 31 | request = b'' |
| 32 | while True: |
| 33 | data = conn.recv(BUFSIZE) |
| 34 | if not data: |
| 35 | break |
| 36 | request += data |
| 37 | reply = execute(request.decode()) |
| 38 | conn.send(reply.encode()) |
| 39 | |
| 40 | def execute(request): |
| 41 | stdout = sys.stdout |