MCPcopy Create free account
hub / github.com/Louisym/MiniCC / proxy_handler

Function proxy_handler

tutorials/14b_networking_for_engineers.py:360–376  ·  view source on GitHub ↗

模拟端口转发

()

Source from the content-addressed store, hash-verified

358 pass
359
360 def proxy_handler():
361 """模拟端口转发"""
362 try:
363 client, addr = proxy_server.accept()
364 data = client.recv(4096)
365
366 # 转发到"容器内部"
367 upstream = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
368 upstream.connect(("127.0.0.1", internal_port))
369 upstream.sendall(data) # 转发请求
370 response = upstream.recv(4096) # 收响应
371 upstream.close()
372
373 client.sendall(response) # 回传给客户端
374 client.close()
375 except (socket.timeout, ConnectionRefusedError):
376 pass
377
378 t1 = threading.Thread(target=internal_handler)
379 t2 = threading.Thread(target=proxy_handler)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected