Skip to content

Instantly share code, notes, and snippets.

@lukemarsden
Created February 27, 2011 21:11
Show Gist options
  • Select an option

  • Save lukemarsden/846545 to your computer and use it in GitHub Desktop.

Select an option

Save lukemarsden/846545 to your computer and use it in GitHub Desktop.

Revisions

  1. lukemarsden revised this gist Feb 27, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions httpRequest.py
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    from twisted.internet import defer
    from twisted.web.client import Agent
    from twisted.web.http_headers import Headers
    import urllib

    class StringProducer(object):
    implements(IBodyProducer)
  2. lukemarsden revised this gist Feb 27, 2011. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions httpRequest.py
    Original file line number Diff line number Diff line change
    @@ -52,10 +52,10 @@ def connectionLost(s, reason):

    # Sample usage:

    d = HybridUtils.httpRequest(
    "http://...",
    {
    'query_arg': 'value',
    },
    headers={'Content-Type': ['application/x-www-form-urlencoded']}
    )
    d = httpRequest(
    "http://...",
    {
    'query_arg': 'value',
    },
    headers={'Content-Type': ['application/x-www-form-urlencoded']}
    )
  3. lukemarsden created this gist Feb 27, 2011.
    61 changes: 61 additions & 0 deletions httpRequest.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    from twisted.web.iweb import IBodyProducer
    from twisted.internet import defer
    from twisted.web.client import Agent
    from twisted.web.http_headers import Headers

    class StringProducer(object):
    implements(IBodyProducer)

    def __init__(self, body):
    self.body = body
    self.length = len(body)

    def startProducing(self, consumer):
    consumer.write(self.body)
    return succeed(None)

    def pauseProducing(self):
    pass

    def stopProducing(self):
    pass

    def httpRequest(url, values={}, headers={}, method='POST'):
    # Construct an Agent.
    agent = Agent(reactor)
    data = urllib.urlencode(values)

    d = agent.request(method,
    url,
    Headers(headers),
    StringProducer(data) if data else None)

    def handle_response(response):
    if response.code == 204:
    d = defer.succeed('')
    else:
    class SimpleReceiver(protocol.Protocol):
    def __init__(s, d):
    s.buf = ''; s.d = d
    def dataReceived(s, data):
    s.buf += data
    def connectionLost(s, reason):
    # TODO: test if reason is twisted.web.client.ResponseDone, if not, do an errback
    s.d.callback(s.buf)

    d = defer.Deferred()
    response.deliverBody(SimpleReceiver(d))
    return d

    d.addCallback(handle_response)
    return d

    # Sample usage:

    d = HybridUtils.httpRequest(
    "http://...",
    {
    'query_arg': 'value',
    },
    headers={'Content-Type': ['application/x-www-form-urlencoded']}
    )