Skip to content

Instantly share code, notes, and snippets.

@solusipse
Last active December 15, 2020 01:39
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save solusipse/6419144 to your computer and use it in GitHub Desktop.
Save solusipse/6419144 to your computer and use it in GitHub Desktop.
Simple echo server written in pure Python
# Example of simple echo server
# www.solusipse.net
import socket
def listen():
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connection.bind(('0.0.0.0', 5555))
connection.listen(10)
while True:
current_connection, address = connection.accept()
while True:
data = current_connection.recv(2048)
if data == 'quit\r\n':
current_connection.shutdown(1)
current_connection.close()
break
elif data == 'stop\r\n':
current_connection.shutdown(1)
current_connection.close()
exit()
elif data:
current_connection.send(data)
print data
if __name__ == "__main__":
try:
listen()
except KeyboardInterrupt:
pass
@ddimass
Copy link

ddimass commented Oct 16, 2017

10clientsserv

@alexvancasper
Copy link

Thanks

@messa
Copy link

messa commented Jul 25, 2019

I think you should use current_connection.sendall(data) instead of current_connection.send(data).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment