Skip to content

Instantly share code, notes, and snippets.

@pykello
Forked from solusipse/echo.py
Created April 28, 2019 07:11
Show Gist options
  • Save pykello/151db8341a37f0d25aeb46150b97d35c to your computer and use it in GitHub Desktop.
Save pykello/151db8341a37f0d25aeb46150b97d35c 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():
socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socket1.bind(('0.0.0.0', 5555))
socket1.listen(10)
while True:
current_connection, address = socket1.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment