Last active
February 28, 2017 06:22
-
-
Save meain/5139d3a199c79e79700067f1e3b00e29 to your computer and use it in GitHub Desktop.
A tiny echo server python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # connect to it using netcat or telnet (nc <ip address> <port>) | |
| import socket | |
| import sys | |
| import threading | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create socket | |
| s.bind(("0.0.0.0",55555)) #binding port | |
| s.listen(1) #listening for only one client | |
| print "Yo kido! We are rolling." | |
| cons = [] | |
| def get(conn): | |
| conn = cons[conn] | |
| while True: | |
| data = conn['conn'].recv(2048) | |
| if data: | |
| print conn['name'][:-1] + ': ' + str(data) | |
| for i in range(len(cons)): | |
| if cons[i]['conn'] is not conn['conn']: | |
| w = conn['name'][:-1] + ': ' + str(data) | |
| cons[i]['conn'].send(w) | |
| while True: | |
| conn, addr = s.accept() | |
| print 'Received connection: ' + str(conn) | |
| if conn not in cons: | |
| t = False | |
| while not t: | |
| conn.send("\nEnter your name: \n") | |
| data = conn.recv(2048) | |
| if len(data) > 2: | |
| t= True | |
| cdata = {} | |
| cdata['conn'] = conn | |
| if data: | |
| cdata['name'] = data | |
| cons.append(cdata) | |
| t = threading.Thread(target=get, args=([cons.index(cdata)]), kwargs={}) | |
| t.daemon = True | |
| t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment