Twisted web client and ssl (Since it took close to an hour to figure this out...)

The secret: twisted.web.client.getPage(url) handles SSL (https://) protocols automatically. Nothing I could find in the documentation mentions that. There are references to the ContextFactory, but nothing is mentioned about whether you need to pass one in or not (you don't). So, a simple script to download an SSL page with Twisted looks something like this:

from twisted.web import client
from twisted.internet import reactor

if __name__ == "__main__":
def do( ):
df = client.getPage(
url = "",
followRedirect = True,
def printOnError( error ):
print 'E', error
def printOnSuccess( page ):
print 'S', page
df.addErrback( printOnError )
df.addCallback( printOnSuccess )
return df
reactor.callWhenRunning( do )

The thing that confused me was that the URLs I was testing with were all invalid, so when the client went to connect, it was receiving 501 errors, which I was assuming were due to a mis-configured SSL connection.

Anyway, think I'll head off to bed so I can get an early start and get in a nice long day of hacking before heading off to PyGTA.


  1. x

    x on 03/31/2005 5:11 a.m. #

    For those of you who don't have the benefit of being able to saunter over to Mike's place and have him solve the Twisted problems you've been stuck on half the day personally... here's a tip... <br />
    <br />
    Make sure you have PyOpenSSL installed! Twisted's traceback message is not going to help you out on figuring this out very easily. Grrr...

Comments are closed.


Pingbacks are closed.