Skip to content

Instantly share code, notes, and snippets.

@SXHRYU
Created November 16, 2023 13:50
Show Gist options
  • Save SXHRYU/c839d13e8d0ca327793aafcf0f8f61cc to your computer and use it in GitHub Desktop.
Save SXHRYU/c839d13e8d0ca327793aafcf0f8f61cc to your computer and use it in GitHub Desktop.
Example of proxy TCP echo server. Useful for debugging and analysing incoming traffic.
# Proxy echo TCP server program
import socket
HOST = "localhost"
PORT = 6667
HOST_NEW = "localhost"
PORT_NEW = 6666
CONNECTED = False
s = socket.socket()
s.bind((HOST, PORT))
s.listen(5)
print("\n--------------\nStart listening")
conn, addr = s.accept()
with conn:
while True:
data = conn.recv(4096)
print(f"-------------\nFROM CLIENT:\n{data}\n-------------\n")
if not CONNECTED:
s2 = socket.socket()
s2.connect((HOST_NEW, PORT_NEW))
CONNECTED = True
s2.sendall(data)
data = s2.recv(4096)
print(
f"------------------\nFROM SERVER:\n{data!r}\n------------------\n"
)
print(r"##################")
conn.send(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment