Quick python socket server that stdouts all input

And here’s a simple python script that allows you to connect to your machine and debug your client output. It basically echoes to stdout everything that gets passed to it. Specially useful to debug HTTP GET requests.

#!/usr/bin/env python
#  Disconnect the client.
#  Stop the server.

import socket
import sys

host = ''
# host = 'localhost' # to restrict to localhost connections only.
port = 3434
pending_connections = 1
buffer_size = 1

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(pending_connections)
try:
  while True:
      print "nnWaiting for connection..."
      client, address = s.accept()
      print "Client connected: " + str(address)
      while True:
        data = client.recv(buffer_size)
        if not data or chr(3) in data:
          break
        elif chr(24) in data:
          sys.exit(0)
        sys.stdout.write(data)
      client.close()
      print "nClient disconnected: " + str(address)
finally:
  s.close()
Advertisement