Skip to content

Instantly share code, notes, and snippets.

@nk0t
Created August 9, 2013 14:50
Show Gist options
  • Save nk0t/6194247 to your computer and use it in GitHub Desktop.
Save nk0t/6194247 to your computer and use it in GitHub Desktop.
simple echo server in python
import socket
import threading
def accept_request(client):
client.send('welcome to echo server\n')
client.send('please input\n')
while True:
msg = client.recv(1024)
if(msg == 'exit\r\n'):
break
client.send(msg)
client.close()
def server_main():
host = socket.gethostbyname('localhost')
port = 12345
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(1)
print 'echo server running on port {0}'.format(port)
while True:
client, address = sock.accept()
thread = threading.Thread(target = accept_request, args = (client,))
thread.start()
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment