Script example for Python to implement Port ing under TCP protocol

Source: Internet
Author: User
The most basic form of port ING is to transfer all data sent from one port to another through an intermediate port, here we will take a look at the example of a script program that implements the port ing function under the TCP protocol in Python. 1 Port ing

For example, the role of port ING is described.

There are three computers A, B, and C. A and B are interconnected, while B and C are interconnected, but A and C are disconnected. at this time, A Web service is opened on C, how can A access C's Web services?

The simplest and most effective method is to open A port ing service on B and then allow A to access A port on B, B forwards all the traffic on this port to the Web service port of C, and forwards all the traffic returned by the Web service to. In this way, A uses B as the stepping stone to indirectly access the Web service on C.

2. implementation process

The principle of port ING is not complex. This article uses TCP as an example to introduce the implementation process and draws a time sequence diagram (as shown below). here we will not describe it in detail.

Note that Port ING only supports traffic forwarding and does not process application-layer data. Therefore, multi-channel protocols (such as FTP) are not supported ).

3 Sample code

Follow the process above to implement Python as follows (we suggest looking forward from the back ):

#-*-Coding: UTF-8-*-# tcp mapping created by huaow (huaow.com) at 2014-08-31import socketimport threading # port ing configuration information pai_remote_ip = '2017. 168.0.10 'cfg _ REMOTE_PORT = 221__local_ip = '0. 0.0.0 'cfg _ LOCAL_PORT = 10022 # received data cache size PKT_BUFF_SIZE = 2048 # debug log encapsulation def send_log (content): print content return # One-way stream data transfer def tcp_mapping_worker (conn_worker er, conn_sender): while True: try: data = conn_receiver.recv (PKT_BUFF_SIZE) failed t Exception: send_log ('event: Connection closed. ') break if not data: send_log ('info: No more data is stored ed. ') break try: conn_sender.sendall (data) Failed t Exception: send_log ('Error: Failed sending data. ') break # send_log ('info: Mapping data> % s' % repr (data) send_log ('info: Mapping> % s-> % s> % d bytes. '% (response (), response (), len (data) conn_receiver.close () conn_sender.close () return # process def tcp_mapping_request (local_conn, remote_ip, remote_port ): remote_conn = socket. socket (socket. AF_INET, socket. SOCK_STREAM) try: remote_conn.connect (remote_ip, remote_port) failed t Exception: local_conn.close () send_log ('Error: Unable to connect to the remote server. ') return threading. thread (target = tcp_mapping_worker, args = (local_conn, remote_conn )). start () threading. thread (target = tcp_mapping_worker, args = (remote_conn, local_conn )). start () return # port ing function def tcp_mapping (remote_ip, remote_port, local_ip, local_port): local_server = socket. socket (socket. AF_INET, socket. SOCK_STREAM) local_server.bind (local_ip, local_port) local_server.listen (5) send_log ('event: Starting mapping service on '+ local_ip +': '+ str (local_port) + '... ') while True: try: (local_conn, local_addr) = local_server.accept () handle T KeyboardInterrupt, Exception: local_server.close () send_log ('event: Stop mapping service. ') break threading. thread (target = tcp_mapping_request, args = (local_conn, remote_ip, remote_port )). start () send_log ('event: Receive mapping request from % s: % d. '% local_addr) return # main function if _ name _ =' _ main _ ': tcp_mapping (pai_remote_ip, pai_remote_port, pai_local_ip, pai_local_port)

4. run

The running effect is as follows. 192.168.0.20 successfully accesses the SSH service (port 22) of 192.168.0.10 by connecting to Port 10022 of the ing server ):

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.