Python implements ultra-simple port forwarding

Source: Internet
Author: User
This article describes how to implement ultra-simple port forwarding in python. The example analyzes the port forwarding technique of Python homogeneous socket, which has some reference value, for more information about how to implement ultra-simple port forwarding in python, see the example in this article. Share it with you for your reference. The details are as follows:

The code is very simple and implements a simple port data forwarding function. you need to modify it for the real environment.

The code is as follows:

# Tcp server
Import socket
Host = '127. 0.0.1 '# Local Server IP
Host2 = '127. 0.0.1 '# Real Server IP
Port = 6001 # Local Server Port
Port2 = 7001 # Real Server Port
Def ProcData (data ):
Return data
# Add more code ....
Print "Map Server start from" + host + ":" + str (port) + "to" + host2 + ":" + str (port2) + "\ r \ n"
Server = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Server. bind ('2014. 0.0.1 ', port ))
Print "127.0.0.1 Server start at" + str (port) + "\ r \ n"
Client = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Client. connect (host2, port2 ))
Print host + "Client connect to" + host2 + ":" + str (port2) + "\ n"
Server. listen (5)
Ss, addr = server. accept ()
Print 'got connected from', addr
While 1:
Msg = ss. recv (20480)
Print "Get:" + repr (msg) + "\ r \ n"
Client. send (msg)
# Print "Client send data % s to" % repr (msg)
Buf = client. recv (20480)
# Print "Client recv data % s from" % repr (buf)
Ss. send (buf)
Print "Send:" + repr (buf) + "\ r \ n"

I hope this article will help you with Python programming.

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.