Skip to content

Instantly share code, notes, and snippets.

@Cediddi
Created September 22, 2017 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cediddi/77d2ad6ca671256614a2821c523769ae to your computer and use it in GitHub Desktop.
Save Cediddi/77d2ad6ca671256614a2821c523769ae to your computer and use it in GitHub Desktop.
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(1024 * 1024 * 10)
headers = data[:data.index(b"\r\n\r\n")]
payload = data[data.index(b"\r\n\r\n") + 4:]
if data:
current_connection.send("HTTP/1.0 200 OK\r\n\r\n".encode())
current_connection.send("Data: {}\r\n".format(payload).encode())
current_connection.send("Size: {}\r\n".format(len(payload)).encode())
print("=" * 79)
print(headers.decode())
print("- " * 40)
if payload:
try:
payload = payload.decode()
print(">>>TEXT-PAYLOAD<<<")
print(payload.strip())
print(">>>TEXT-PAYLOAD<<<")
except UnicodeDecodeError:
print(">>>BINARY-PAYLOAD<<<")
print("SIZE: {}".format(len(payload)))
print(">>>BINARY-PAYLOAD<<<")
else:
print(">>>NO-PAYLOAD<<<")
print("=" * 79)
current_connection.close()
break
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