[Python-twisted] port forwarding server for getting started with twisted

Source: Internet
Author: User
Tags windows remote desktop

Twisted is a well-known network I/O framework based on Asynchronous events in the python field. It has excellent performance and has been tested by BT. I have been coveted for a long time, so I wrote a port forwarding server ~~~

 

Requirement: Forward Windows Remote Desktop to a port.

That is, the remote desktop service is enabled on a B C. C and port 3389 is enabled on the three machines.

Run the port forwarding program on B to send the data sent to port 1099 of B to port 3389 of C.

In this way, Remote Desktop client a can access port 1099 of port B to remotely access machine C.

Understand?

Let's go !!

Code:

From twisted. Internet. Protocol import protocol, clientcreator

From twisted. Internet import Reactor
From twisted. Protocols. Basic import linereceiver
From twisted. Internet. Protocol import factory, clientfactory
Class transfer (Protocol ):
Def _ init _ (Self ):
Pass
Def connectionmade (Self ):
C = clientcreator (reactor, clienttransfer)
C. connecttcp ("10.61.1.243", 3389). addcallback (self. set_protocol)
Self. Transport. pauseproducing ()

Def set_protocol (self, p ):
Self. Server = P
P. set_protocol (Self)

Def datareceived (self, data ):
Self. server. Transport. Write (data)

Def connectionlost (self, reason ):
Self. Transport. loseconnection ()
Self. server. Transport. loseconnection ()

Class clienttransfer (Protocol ):
Def _ init _ (Self ):
Pass

Def set_protocol (self, p ):
Self. Server = P
Self. server. Transport. resumeproducing ()
Pass
Def datareceived (self, data ):
Self. server. Transport. Write (data)
Pass

Factory = Factory ()
Factory. Protocol = Transfer
Reactor. listentcp (1099, factory)
Reactor. Run ()
The written red code is the destination address and port of the forwarding.

I tested it. The Remote Desktop is normal. My implementation is quite evil. If there is no architecture scalability, let's take a look.

This program is suitable for beginners, including the writing of servers and clients. Pay attention to the green mark. I didn't pay attention to it at the beginning,

As a result, the server in self. server. Transport. Write (data) starts to write data before initialization.

O ~ ..

After reading the source code of twisted, I found a portforword. py under twisted. Protocols. base.

Post it:
# Copyright (c) 2001-2004 twisted matrix laboratories.
# See license for details.

"""
A simple port forwarder.
"""

# Twisted imports
From twisted. Internet import Protocol
From twisted. Python import log

Class proxy (protocol. Protocol ):
Noisy = true

Peer = none

Def setpeer (self, peer ):
Self. Peer = peer

Def connectionlost (self, reason ):
If self. Peer is not none:
Self. Peer. Transport. loseconnection ()
Self. Peer = none
Elif self. Noisy:
Log. MSG ("unable to connect to peer: % s" % (reason ,))

Def datareceived (self, data ):
Self. Peer. Transport. Write (data)

Class proxyclient (proxy ):
Def connectionmade (Self ):
Self. Peer. setpeer (Self)
# We're connected, everybody can read to their hearts content.
Self. Peer. Transport. resumeproducing ()

Class proxyclientfactory (protocol. clientfactory ):

Protocol = proxyclient

Def setserver (self, server ):
Self. Server = Server

Def buildprotocol (self, * ARGs, ** kW ):
Prot = protocol. clientfactory. buildprotocol (self, * ARGs, ** kW)
Prot. setpeer (self. Server)
Return prot

Def clientconnectionfailed (self, connector, reason ):
Self. server. Transport. loseconnection ()

Class proxyserver (proxy ):

Clientprotocolfactory = proxyclientfactory

Def connectionmade (Self ):
# Don't read anything from the connecting client until we have
# Somewhere to send it.
Self. Transport. pauseproducing ()

Client = self. clientprotocolfactory ()
Client. setserver (Self)

From twisted. Internet import Reactor
Reactor. connecttcp (self. Factory. Host, self. Factory. Port, client)

Class proxyfactory (protocol. Factory ):
"Factory for port forwarder ."""

Protocol = proxyserver

Def _ init _ (self, host, Port ):
Self. Host = Host
Self. Port = port

Features are the same, but they are more elegant. Study hard ....

Keep going...

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.