[Twisted-Python] Reverse HTTP proxy code review

Kevin Horn kevin.horn at gmail.com
Tue Mar 2 16:17:41 EST 2010


On Tue, Mar 2, 2010 at 3:00 PM, Fred C <fred at bsdhost.net> wrote:

>
> On Mar 2, 2010, at 12:45 PM, Kevin Horn wrote:
>
> On Tue, Mar 2, 2010 at 1:19 PM, Fred C <fred at bsdhost.net> wrote:
>
>>
>> Hello,
>>
>> I am totally new with Twisted and this the first code I have ever written
>> using that package. One of my main problem was to figure out how to fit
>> things together and I am not even sure I understand everything I am doing
>> here.
>>
>> Anyway, for a project I have to write a proxy that figure out where is
>> located a resource by requesting a database. then figure out if the resource
>> is accessible and them open a proxy connection to download that resource.
>>
>> I was wandering if there is someone in that mailing list who can review
>> that code and tell me how I can improve things. I know I am not accessing
>> the database the right way but I can't figure out how to work with the
>> Callbacks.
>>
>> Any help or advice will be very much appreciated. The code is available
>> here: http://pastebin.com/BGcqzh4Y
>>
>> -fred-
>>
>>
> I don't really use Twisted for web work, so I don't have much of a comment
> on that, but for database access you probably want to use
> `twisted.enterprise.adbapi`.
>
>
>
> That was of the thing I know it's wrong with my code. I have to use adbapi
> and have a call back to get the result from the database. My main problem in
> that case is I don't know how to have the caller wait for the response from
> the database.
>
> Thanks for taking time to look at that code and for your reply.
>
> -fred-
> http://kiq.me/oi
>
>
>
Have a look at:
http://twistedsphinx.funsize.net/projects/core/howto/rdbms.html#how-do-i-use-adbapi

Basically, when you call runQuery, you get back a deferred object.  You'll
want to add a callback to that using something like:

def get_stuff_from_db():
    d = dbpool.runQuery('Your SQL Here')
    d.addCallback(your_callback_function)

def your_callback_function(results):
    # do some stuff with your results

so when the DB query completes, your_callback_function will get called
automatically by the deferred object.

Obviously this is very rough, probably incorrect code, but hopefully it's
enough to get you started.

Kevin Horn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100302/59b34011/attachment.htm 


More information about the Twisted-Python mailing list