Skip to content

Instantly share code, notes, and snippets.

@kylegibson
Created October 28, 2010 06:33
Show Gist options
  • Save kylegibson/650771 to your computer and use it in GitHub Desktop.
Save kylegibson/650771 to your computer and use it in GitHub Desktop.
Python Single Connection Echo Server
#!/usr/bin/python
import socket
import os
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
f = open("echo_server.pid", "w")
f.write(str(os.getpid()))
f.close()
while True:
conn, addr = s.accept()
print 'Connected by', addr
try:
while True:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
except:
pass
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment