Socket port redirection (Python example)

Source: Internet
Author: User

Scenario: on the port12345Enable file sharing.8000The port also accesses this share.

According to my previous articleArticle(Http://www.cnblogs.com/MikeZhang/archive/2012/02/11/httpFileSharePythonAndGo.html),

You can easily12345Enable sharing. The effect is as follows:

 

 

To implement the desired function, you only need to redirect the port,CodeAs follows:

 

#!
/usr/bin/python
'''
file:
redirect. PY
author:
Mike
Email:
Mike_Zhang@live.com
'''

Import Socket, OS
Buflen
= 4*1024

Sock1
= Socket. socket (socket. af_inet,
Socket. sock_stream)
Sock1.bind (('192.168.168.100',
8000 ))
Sock1.listen (5)

Sock2
= Socket. socket (socket. af_inet,
Socket. sock_stream)
Sock2.connect (('192.168.168.100',
12345 ))

While   True:
Connection, address
= Sock1.accept ()
Buf
= Connection. Recv (buflen)
# Print
Buf
Sock2.send (BUF)
Connection. Send (sock2.recv (buflen ))
Connection. Close ()

 

Running effect:

 

Okay, that's all. I hope it will help you.

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.