Skip to content

Instantly share code, notes, and snippets.

@adiroiban adiroiban/test.py
Created Mar 10, 2015

Embed
What would you like to do?
from __future__ import print_function
import json
from StringIO import StringIO
from twisted.internet import defer, protocol, reactor, task
from twisted.web import server, static
from twisted.web.client import (
Agent,
FileBodyProducer,
HTTPConnectionPool,
PartialDownloadError,
ResponseDone,
)
from twisted.web.http import Headers, PotentialDataLoss
URL = 'http://localhost:8080/'
class PersistentAgent(Agent):
"""
HTTP Client agent with a connection pool.
"""
_reactor = reactor
def __init__(self, pool_size=3):
pool = HTTPConnectionPool(self._reactor, persistent=True)
pool.retryAutomatically = False
pool.maxPersistentPerHost = pool_size
super(PersistentAgent, self).__init__(reactor=self._reactor, pool=pool)
def postJSON(self, url, body, headers=None):
"""
Make a POST request with JSON body.
"""
if headers is None:
headers = {'Content-Type': ['application/json']}
return self.request(
method='POST',
uri=url.encode('utf-8'),
headers=Headers(headers),
bodyProducer=FileBodyProducer(StringIO(json.dumps(body)))
)
def print_response(response):
print(response.code)
print(response._transport._producer.getHost())
def start():
agent = PersistentAgent()
d = agent.postJSON(URL, {})
d.addCallback(print_response)
# Calling without a delay seems to work as a new connection is made.
# Don't know why connection is not yet recycled at this stage.
#d.addCallback(lambda _: agent.postJSON(URL, {}))
# Calling with a delay will allow the previous connection to be put
# back in the connections lists and be reused.
d.addCallback(lambda _: task.deferLater(reactor, 0.5, agent.postJSON, URL, {}))
d.addCallback(print_response)
d.addErrback(lambda failure: print(failure.value))
d.addBoth(lambda _: reactor.stop())
site = server.Site(static.File('.'))
reactor.listenTCP(8080, site)
reactor.callWhenRunning(start)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.