


Python implements port forwarding and redirection examples under TCP/IP protocol
First, we use webpy to write a simple website, listen to port 8080, and return the "Hello, EverET.org" page.
Then we use our forwarding.py to establish two communication pipes between port 80 and port 8080 for two-way communication.
At this point, we access our server through port 80.
The browser gets:
Then, we can see the communication content between the browser and webpy in the output of forwarding.py.
Code:
#!/usr/bin/env python import sys, socket, time, threading loglock = threading.Lock() def log(msg): loglock.acquire() try: print '[%s]: \n%s\n' % (time.ctime(), msg.strip()) sys.stdout.flush() finally: loglock.release() class PipeThread(threading.Thread): def __init__(self, source, target): threading.Thread.__init__(self) self.source = source self.target = target def run(self): while True: try: data = self.source.recv(1024) log(data) if not data: break self.target.send(data) except: break log('PipeThread done') class Forwarding(threading.Thread): def __init__(self, port, targethost, targetport): threading.Thread.__init__(self) self.targethost = targethost self.targetport = targetport self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.bind(('0.0.0.0', port)) self.sock.listen(10) def run(self): while True: client_fd, client_addr = self.sock.accept() target_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) target_fd.connect((self.targethost, self.targetport)) log('new connect') # two direct pipe PipeThread(target_fd, client_fd).start() PipeThread(client_fd, target_fd).start() if __name__ == '__main__': print 'Starting' import sys try: port = int(sys.argv[1]) targethost = sys.argv[2] try: targetport = int(sys.argv[3]) except IndexError: targetport = port except (ValueError, IndexError): print 'Usage: %s port targethost [targetport]' % sys.argv[0] sys.exit(1) #sys.stdout = open('forwaring.log', 'w') Forwarding(port, targethost, targetport).start()

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
