473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Twisted - extending portforward (simple example)

I hit a problem yesterday with my mail connection. In a desparate
attempt to understand what was going on, I wanted to log the
connection traffic. After a bit of searching, I found a post on c.l.p
from Andrew Bennetts explaining how to run a port forwarder in 2 lines
using Twisted.

$ mktap portforward -p 8000 -h remote -d 20
$ twistd -f portforward.tap

This looked brilliant - all I needed to do was add logging. A quick
look (I was *very* short of time, so I couldn't spend long) showed me
no obvious way of adding a hook (at least, not one which wouldn't turn
the above 2-liner into something more complex, that I didn't have time
to work out...) So I gave in, and just added a print statement in the
twisted code.

This gave me the results I wanted, but left me wondering (and not
getting far in finding out!) how I *should* have done this.

Ideally, I was expecting to be able to write a small .py file,
importing and minimally overriding portforward.py, and then be able to
do the mktap/twistd thing. But I couldn't see how.

Can anyone give me a simple example of how I should have done this?
The logging portforwarder example seems like a nice simple starting
point, from which I can go on and learn how to do clever stuff :-)

Thanks,
Paul.

PS Many thanks to the twisted crew - even if I didn't do things the
"right" way, I got my problem diagnosed in no time at all, when I'd
previously struggled with it for days!
Jul 18 '05 #1
4 4133
On Fri, Jun 27, 2003 at 03:42:20AM -0700, Paul Moore wrote:
I hit a problem yesterday with my mail connection. In a desparate
attempt to understand what was going on, I wanted to log the
connection traffic. After a bit of searching, I found a post on c.l.p
from Andrew Bennetts explaining how to run a port forwarder in 2 lines
using Twisted.

$ mktap portforward -p 8000 -h remote -d 20
$ twistd -f portforward.tap

This looked brilliant - all I needed to do was add logging. A quick
look (I was *very* short of time, so I couldn't spend long) showed me
no obvious way of adding a hook (at least, not one which wouldn't turn
the above 2-liner into something more complex, that I didn't have time
to work out...) So I gave in, and just added a print statement in the
twisted code.

This gave me the results I wanted, but left me wondering (and not
getting far in finding out!) how I *should* have done this.

Ideally, I was expecting to be able to write a small .py file,
importing and minimally overriding portforward.py, and then be able to
do the mktap/twistd thing. But I couldn't see how.
The easiest way is probably to do this:

--- loggingpf.py ---

from twisted.internet import app
from twisted.python import log
from twisted.protocols import portforward
from twisted.tap.portforward import Options

class LoggingProxyClient(portforward.ProxyClient):
def dataReceived(self, data):
log.msg('server sent: ' + repr(data))
portforward.ProxyClient.dataReceived(self, data)

class LoggingProxyClientFactory(portforward.ProxyClientF actory):
protocol = LoggingProxyClient

class LoggingProxyServer(portforward.ProxyServer):
clientProtocolFactory = LoggingProxyClientFactory

def dataReceived(self, data):
log.msg('client sent: ' + repr(data))
portforward.ProxyServer.dataReceived(self, data)

class LoggingProxyFactory(portforward.ProxyFactory):
protocol = LoggingProxyServer

application = app.Application('LoggingPortForward')
application.listenTCP(8000, LoggingProxyFactory('localhost', 80))

--- end --

Then you can use

$ twistd -n -o -y loggingpf.py

You could easily adapt this to become a mktap plugin; see

http://twistedmatrix.com/documents/howto/plugin

(For instance, the normal portforward plugin is defined in
twisted/plugins.tml and twisted/tap/portforward.py)

An even lazier approach would be to add an "application.run(save=0)" to the
end of loggingpf.py -- then you'd be able to run it directly as

$ python loggingpf.py

(but using twistd gives you the ability to daemonise, use log files, etc).
Can anyone give me a simple example of how I should have done this?
The logging portforwarder example seems like a nice simple starting
point, from which I can go on and learn how to do clever stuff :-)


Unfortunately, it's not very simple, as you can see -- although you only
need to override two methods (ProxyClient.dataReceived and
ProxyServer.dataReceived), you need to make four subclasses to tie it all
together.

-Andrew.
Jul 18 '05 #2
In article <ma*********************************@python.org> , Andrew Bennetts:
On Fri, Jun 27, 2003 at 03:42:20AM -0700, Paul Moore wrote:
I hit a problem yesterday with my mail connection. In a desparate
attempt to understand what was going on, I wanted to log the
connection traffic. After a bit of searching, I found a post on c.l.p
from Andrew Bennetts explaining how to run a port forwarder in 2 lines
using Twisted.

$ mktap portforward -p 8000 -h remote -d 20
$ twistd -f portforward.tap

This looked brilliant - all I needed to do was add logging. A quick
look (I was *very* short of time, so I couldn't spend long) showed me
no obvious way of adding a hook (at least, not one which wouldn't turn
the above 2-liner into something more complex, that I didn't have time
to work out...) So I gave in, and just added a print statement in the
twisted code.

This gave me the results I wanted, but left me wondering (and not
getting far in finding out!) how I *should* have done this.

Ideally, I was expecting to be able to write a small .py file,
importing and minimally overriding portforward.py, and then be able to
do the mktap/twistd thing. But I couldn't see how.
The easiest way is probably to do this:

--- loggingpf.py ---


Unfortunately, it's not very simple, as you can see -- although you only
need to override two methods (ProxyClient.dataReceived and
ProxyServer.dataReceived), you need to make four subclasses to tie it all
together.

I was thinking about this too, and I was wondering if it might be good to
add a "verbosity" switch to either mktap or twistd, along the lines of
what netcat or ssh does. (ie, include 2 or 3 -v switches for additional
verbosity). It seems like it would definitely be helpful in certain
situations.

Jul 18 '05 #3
On 27 Jun 2003 03:42:20 -0700, Paul Moore <pa********@atosorigin.com>
wrote:
I hit a problem yesterday with my mail connection.


Well, if you are unlucky enough to be a Bell Sympatico customer (like me)
on the 26th the blocked all traffic on port 25 without telling anybody. It
was really rather nice of them...

Gee, let's first raise the rates for the same service, then put a bandwidth
cap on it, and now block port 25...and see how many people get pissed.

--
Markus
Jul 18 '05 #4
"Markus Wankus" <ma***********@hotmail.com> wrote in message
news:op**************@news1.on.sympatico.ca...
On 27 Jun 2003 03:42:20 -0700, Paul Moore <pa********@atosorigin.com>
wrote:
I hit a problem yesterday with my mail connection.
Well, if you are unlucky enough to be a Bell Sympatico customer (like me)
on the 26th the blocked all traffic on port 25 without telling anybody.

It was really rather nice of them...

Gee, let's first raise the rates for the same service, then put a bandwidth cap on it, and now block port 25...and see how many people get pissed.


Yeah, Cox recently installed a block on outgoing port 25 connections. Their
justification is "it helps us to reduce the amount of spam coming out of our
domain". At least they will let me use any email address I like as long as I
connect to their SMTP servers from inside their network, but unfotunately
they don't appear to have any way to let me do the same thing when I'm
somewhere else (like a customer location), so they've effectively forced me
to use a dual setup for my mail.

All done without any advance notice, of course. Internet "access" takes on a
whole new meaning in these guys' hands :-)
regards
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: mir nazim | last post by:
hi, i m planning to start writing intranet applications and want ur real cool advices for choosing the correct platform. the choice is between the three: 1. Twisted 2. Medusa 3. Zope (i do...
2
by: Qp | last post by:
Hello. I'm building a simple chat server and client interface, and I've got everything working except for this: While the client's basic.LineReceiver protocol class can sendLine when a...
1
by: Joakim Bech | last post by:
At this page --> http://twistedmatrix.com/documents/current/howto/udp is an example that shows how connected UDP is done with Twisted. I can't get it to work. When I run it, it says: .......
2
by: Taki Jeden | last post by:
Hi Anybody used wxPython with twisted? I started putting together a Twisted-based app with wx GUI, and the widgets just don't work - some controls do not show up etc. - at least on my system....
2
by: SeSe | last post by:
Hi, I am new to Twisted. I use a Twisted 1.3.0 on MS Windows XP Home Edition, my python version is 2.3 I try the TCP echoserv.py and echoclient.py example. But the client always fail with...
1
by: Mateusz Sołtysek | last post by:
Hi call, Does anybody know, if there is any opensource, working FTP server implementation based on Twisted framework? Greetings
1
by: qwejohn | last post by:
Hello, I had posted this question in the twisted mailing list but did not got a solution ; I hope that the python Gurus of this forum can help me a bit. I am trying the exmaple in the python...
0
by: James Mills | last post by:
On Fri, Sep 19, 2008 at 6:24 AM, Fredrik Lundh <fredrik@pythonware.comwrote: There is also one more alternative: pymills - current version 3.4 It has a different design goal to twisted and...
0
by: Stodge | last post by:
I'm trying to get a simple multicast application working using Twisted; so far I have: from twisted.internet.protocol import DatagramProtocol from twisted.internet import reactor from...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.