Speno's Pythonic Avocado 2004/2

2004-02-18

Teaching Twisted

I gave a short presentation at work today on the basics of Twisted. I tried to cover what I consider Twisted's core networking features: Protocols, Transports, Reactors, Factories and Deferreds.

Here's the same code I used to demonstrate the first four of those items:

from twisted.internet import protocol from twisted.internet import reactor class CountingEchoProtocol(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) self.factory.incrementCount(len(data)) data_written = self.factory.getCount() self.transport.write('Wrote %d bytes so far.\r\n' % data_written) class CountingEchoFactory(protocol.ServerFactory): protocol = CountingEchoProtocol def __init__(self, max=24): self.count = 0 self.max = max def incrementCount(self, length): self.count = self.count + length if self.count >= self.max: reactor.stop() def getCount(self): return self.count ce_factory = CountingEchoFactory(25) reactor.listenTCP(2323, ce_factory) reactor.run() print 'Wrote %d bytes total.' % ce_factory.getCount()

In prepartion for giving this informal talk, I spent about an hour or so just digging through the Twisted docs and/or the code answering my own questions. So hopefully my co-workers and I all got something from this.

Take care.

posted at 20:57:20    #    comment []    trackback []
 
2004-02-06

Help Wanted

My employer is looking for someone to help me out at work for a short time, maybe a month or two. Requirements including knowing your way around Python, MySQL, CGI, HTML, and UNIX. We prefer someone who can be on site. If we can't get someone soonish, then we'll probably delay the position until May.

If you are interested, drop me a line at spamfilter@macspeno.com.

Take care.

posted at 21:01:36    #    comment []    trackback []
 
2004-02-05

Two Python SNMP packages updated

Two python SNMP packages got updates today. First, there's Ilya Etingof's PySNMP which is now at version 3.4.1. Ilya's also broken the "example" applications out of the main package and called it pysnmp-apps-0.0.1. You can find them both here .

The second update comes from Mike Fletcher who released version 0.2.0 of his TwistedSNMP implementation. This is an SNMP handler for Twisted , which happens to use PySNMP. The new version has a mass-query program which can query a bunch of agents at once. I'm very excited to try it out in my own projects soon.

Thank you Ilya. Thank you Mike. You guys make me look good.

This post references topics: python
posted at 22:31:12    #    comment []    trackback []
 
2004-02-04

Popen5: useful!

I've been playing with Peter Astrand's Popen5 module lately. It's really nice and I hope that it will be accepted into Python's standard library eventually via PEP 324 .

I like this module because it allows nice and easy control over child processes. I get full control of the child's stdin, stdout, and stderr without having to do the dup dance (which I'd always have to lookup before this). And I can make an easy timeout without having to use SIGALRM.

I especially appreciate Popen5 in light of having spent way too much time tring to get a simple return value out of Twisted's utils.getProcessValue() last weekend. But that was mostly my fault. Smiley

Here's an example of using Popen5:

import popen5 import time import os import signal def main(): wait_time = 5 # seconds args = ['/sbin/ping', '-i', '10', 'localhost'] outf = open('ping.out', 'w+') errf = open('ping.err', 'w+') ping = popen5.Popen(args, stdout=outf, stderr=errf) print 'ping started with pid ', ping.pid time_done = time.time() + wait_time while ping.poll() == -1: if time.time() > time_done: print 'bored now!' os.kill(ping.pid, signal.SIGINT) time.sleep(1) os.waitpid(ping.pid, os.WNOHANG) break else: time.sleep(1) for f in [outf, errf]: f.seek(0) output = f.read() if output: print 'Contents of %s:' % f.name print output if __name__ == '__main__': main()

Take care.

P.S. Thanks to everyone who's left comments here. I'm learning a lot, and feel more like a member of the global Python community now. Since I won't make it to either PyCon or OSCON this year, I'm glad I have this.

This post references topics: python
posted at 23:41:36    #    comment []    trackback []
February 2004
MoTuWeThFrSaSu
       1
2 3 4 5 6 7 8
9101112131415
16171819202122
23242526272829
Jan
2004
 Mar
2004

One python programmer's search for understanding and avocados. This isn't personal, only pythonic.

XML-Image Letterimage

© 2004-2005, John P. Speno