Python's method for implementing super simple port forwarding _python

Source: Internet
Author: User
Tags server port

The example in this article describes Python's method for implementing super simple port forwarding. Share to everyone for your reference. Specifically as follows:

The code is very simple, the implementation of a simple port data forwarding function, for the real environment also need to be modified.

Copy Code code 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" + Host + ":" + str (port) + "to" + Host2 + ":" + str (PORT2) + "\ r \ n"
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Server.bind (' 127.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 your Python programming.

Related Article

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.