Example of implementing socket port redirection in python

Source: Internet
Author: User
This article mainly introduces the socket Port Redirection example implemented by python. the implementation function is to enable file sharing on port 12345, at this time, I also want to access this sharing through port 8000 at the same time, which can be easily enabled on port 12345, the effect is as follows:

To implement the desired function, you only need to redirect the port. the code is as follows:

The code is as follows:


#! /Usr/bin/python
'''
File: redirect. py
Author: Mike
'''

Import socket, OS
BufLen = 4*1024

Sock1 = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock1.bind ('192. 168.168.100 ', 192 ))
Sock1.listen (5)

Sock2 = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sock2.connect ('192. 168.168.100 ', 192 ))

While True:
Connection, address = sock1.accept ()
Buf = connection. recv (bufLen)
# Print buf
Sock2.send (buf)
Connection. send (sock2.recv (bufLen ))
Connection. close ()

Running effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.