Created
November 16, 2023 13:50
-
-
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.
This file contains 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
# 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