Ticket #6162: unfinished_examples.patch

File unfinished_examples.patch, 12.8 KB (added by Lasse, 6 years ago)

The exmaple running on localhost needs testing, the there are a few left embedded in HTML

  • doc/web/howto/client.xhtml

    diff --git doc/web/howto/client.xhtml doc/web/howto/client.xhtml
    index 9471407..189fcb3 100644
     
    288288      Here's an example which shows how to use <code>Agent</code> to request
    289289      an <em>HTTPS</em> URL with no certificate verification.
    290290    </p>
    291 
    292     <pre class="python">
    293 from twisted.python.log import err
    294 from twisted.web.client import Agent
    295 from twisted.internet import reactor
    296 from twisted.internet.ssl import ClientContextFactory
    297 
    298 class WebClientContextFactory(ClientContextFactory):
    299     def getContext(self, hostname, port):
    300         return ClientContextFactory.getContext(self)
    301 
    302 def display(response):
    303     print "Received response"
    304     print response
    305 
    306 def main():
    307     contextFactory = WebClientContextFactory()
    308     agent = Agent(reactor, contextFactory)
    309     d = agent.request("GET", "https://example.com/")
    310     d.addCallbacks(display, err)
    311     d.addCallback(lambda ignored: reactor.stop())
    312     reactor.run()
    313 
    314 if __name__ == "__main__":
    315     main()
    316     </pre>
     291       
     292        <a href="listings/client/requestHTTPS.py" class="py-listing">
     293      Request an HTTPS URL with no certificate verification..
     294    </a>
    317295
    318296    <p>
    319297      The important point to notice here is that <code>getContext</code> now
    if __name__ == "__main__": 
    356334      Here's an example which sends requests over a persistent connection:
    357335    </p>
    358336
    359     <pre class="python">
    360 from twisted.internet import reactor
    361 from twisted.internet.defer import Deferred, DeferredList
    362 from twisted.internet.protocol import Protocol
    363 from twisted.web.client import Agent, HTTPConnectionPool
    364 
    365 class IgnoreBody(Protocol):
    366     def __init__(self, deferred):
    367         self.deferred = deferred
    368 
    369     def dataReceived(self, bytes):
    370         pass
    371 
    372     def connectionLost(self, reason):
    373         self.deferred.callback(None)
    374 
    375 
    376 def cbRequest(response):
    377     print 'Response code:', response.code
    378     finished = Deferred()
    379     response.deliverBody(IgnoreBody(finished))
    380     return finished
    381 
    382 pool = HTTPConnectionPool(reactor)
    383 agent = Agent(reactor, pool=pool)
    384 
    385 def requestGet(url):
    386     d = agent.request('GET', url)
    387     d.addCallback(cbRequest)
    388     return d
    389 
    390 # Two requests to the same host:
    391 d = requestGet('http://localhost:8080/foo').addCallback(
    392     lambda ign: requestGet("http://localhost:8080/bar"))
    393 def cbShutdown(ignored):
    394     reactor.stop()
    395 d.addCallback(cbShutdown)
    396 
    397 reactor.run()
    398     </pre>
     337        <a href="listings/client/persistentHTTP.py" class="py-listing">
     338      Requests over a persistent connection
     339    </a>
    399340
    400341    <p>
    401342      Here, the two requests are to the same host, one after the each
  • doc/web/howto/listings/client/cookies.py

    diff --git doc/web/howto/listings/client/cookies.py doc/web/howto/listings/client/cookies.py
    index 80f84c2..57eded2 100644
     
    11from cookielib import CookieJar
    22
    3 from twisted.internet import reactor
     3from twisted.internet import task
    44from twisted.python import log
    55from twisted.web.client import Agent, CookieAgent
    66
    def displayCookies(response, cookieJar): 
    1111    for cookie in cookieJar:
    1212        print cookie
    1313
    14 def main():
     14def main(reactor):
    1515    cookieJar = CookieJar()
    1616    agent = CookieAgent(Agent(reactor), cookieJar)
    1717
    1818    d = agent.request('GET', 'http://www.google.com/')
    1919    d.addCallback(displayCookies, cookieJar)
    2020    d.addErrback(log.err)
    21     d.addCallback(lambda ignored: reactor.stop())
    22     reactor.run()
     21
     22    return d
    2323
    2424if __name__ == "__main__":
    25     main()
     25    task.react(main)
     26 No newline at end of file
  • doc/web/howto/listings/client/filesendbody.py

    diff --git doc/web/howto/listings/client/filesendbody.py doc/web/howto/listings/client/filesendbody.py
    index ed26238..2a31961 100644
     
    11from StringIO import StringIO
    22
    3 from twisted.internet import reactor
     3from twisted.internet import task
    44from twisted.web.client import Agent
    55from twisted.web.http_headers import Headers
    66
    77from twisted.web.client import FileBodyProducer
    88
    9 agent = Agent(reactor)
    10 body = FileBodyProducer(StringIO("hello, world"))
    11 d = agent.request(
    12     'GET',
    13     'http://example.com/',
    14     Headers({'User-Agent': ['Twisted Web Client Example'],
    15              'Content-Type': ['text/x-greeting']}),
    16     body)
     9def main(reactor):
     10    agent = Agent(reactor)
     11    body = FileBodyProducer(StringIO("hello, world"))
     12    d = agent.request(
     13        'GET',
     14        'http://example.com/',
     15        Headers({'User-Agent': ['Twisted Web Client Example'],
     16                 'Content-Type': ['text/x-greeting']}),
     17        body)
    1718
    18 def cbResponse(ignored):
    19     print 'Response received'
    20 d.addCallback(cbResponse)
     19    def cbResponse(ignored):
     20        print 'Response received'
     21    d.addCallback(cbResponse)
    2122
    22 def cbShutdown(ignored):
    23     reactor.stop()
    24 d.addBoth(cbShutdown)
     23    return d
    2524
    26 reactor.run()
     25task.react(main)
     26 No newline at end of file
  • doc/web/howto/listings/client/gzipdecoder.py

    diff --git doc/web/howto/listings/client/gzipdecoder.py doc/web/howto/listings/client/gzipdecoder.py
    index dc6af43..4129ce9 100644
     
    11from twisted.python import log
    2 from twisted.internet import reactor
     2from twisted.internet import task
    33from twisted.internet.defer import Deferred
    44from twisted.internet.protocol import Protocol
    55from twisted.web.client import Agent, ContentDecoderAgent, GzipDecoder
    class BeginningPrinter(Protocol): 
    99        self.finished = finished
    1010        self.remaining = 1024 * 10
    1111
    12 
    1312    def dataReceived(self, bytes):
    1413        if self.remaining:
    1514            display = bytes[:self.remaining]
    class BeginningPrinter(Protocol): 
    1716            print display
    1817            self.remaining -= len(display)
    1918
    20 
    2119    def connectionLost(self, reason):
    2220        print 'Finished receiving body:', reason.type, reason.value
    2321        self.finished.callback(None)
    2422
    25 
    26 
    2723def printBody(response):
    2824    finished = Deferred()
    2925    response.deliverBody(BeginningPrinter(finished))
    3026    return finished
    3127
    32 
    33 def main():
     28def main(reactor):
    3429    agent = ContentDecoderAgent(Agent(reactor), [('gzip', GzipDecoder)])
    3530
    3631    d = agent.request('GET', 'http://www.yahoo.com/')
    3732    d.addCallback(printBody)
    3833    d.addErrback(log.err)
    39     d.addCallback(lambda ignored: reactor.stop())
    40     reactor.run()
     34
     35    return d
    4136
    4237if __name__ == "__main__":
    43     main()
     38    task.react(main)
  • new file doc/web/howto/listings/client/persistentHTTP.py

    diff --git doc/web/howto/listings/client/persistentHTTP.py doc/web/howto/listings/client/persistentHTTP.py
    new file mode 100644
    index 0000000..b252885
    - +  
     1from twisted.internet import task
     2from twisted.internet.defer import Deferred, DeferredList
     3from twisted.internet.protocol import Protocol
     4from twisted.web.client import Agent, HTTPConnectionPool
     5
     6class IgnoreBody(Protocol):
     7    def __init__(self, deferred):
     8        self.deferred = deferred
     9
     10    def dataReceived(self, bytes):
     11        pass
     12
     13    def connectionLost(self, reason):
     14        self.deferred.callback(None)
     15
     16
     17def cbRequest(response):
     18    print 'Response code:', response.code
     19    finished = Deferred()
     20    response.deliverBody(IgnoreBody(finished))
     21    return finished
     22
     23def main(reactor):
     24
     25    pool = HTTPConnectionPool(reactor)
     26    agent = Agent(reactor, pool=pool)
     27   
     28    def requestGet(url):
     29        d = agent.request('GET', url)
     30        d.addCallback(cbRequest)
     31        return d
     32    # Two requests to the same host:
     33    d = requestGet('http://localhost:8080/foo').addCallback(
     34    lambda ign: requestGet("http://localhost:8080/bar"))
     35   
     36    return d
     37   
     38task.react(main)
     39 No newline at end of file
  • doc/web/howto/listings/client/request.py

    diff --git doc/web/howto/listings/client/request.py doc/web/howto/listings/client/request.py
    index 4931863..5616764 100644
     
    1 from twisted.internet import reactor
     1from twisted.internet import task
    22from twisted.web.client import Agent
    33from twisted.web.http_headers import Headers
    44
    5 agent = Agent(reactor)
     5def main(reactor):
     6    agent = Agent(reactor)
     7    d = agent.request(
     8        'GET',
     9        'http://google.com/',
     10        Headers({'User-Agent': ['Twisted Web Client Example']}),
     11        None)
    612
    7 d = agent.request(
    8     'GET',
    9     'http://example.com/',
    10     Headers({'User-Agent': ['Twisted Web Client Example']}),
    11     None)
     13    def cbResponse(ignored):
     14        print 'Response received'
     15    d.addCallback(cbResponse)
    1216
    13 def cbResponse(ignored):
    14     print 'Response received'
    15 d.addCallback(cbResponse)
     17    return d
    1618
    17 def cbShutdown(ignored):
    18     reactor.stop()
    19 d.addBoth(cbShutdown)
    20 
    21 reactor.run()
     19task.react(main)
     20 No newline at end of file
  • new file doc/web/howto/listings/client/requestHTTPS.py

    diff --git doc/web/howto/listings/client/requestHTTPS.py doc/web/howto/listings/client/requestHTTPS.py
    new file mode 100644
    index 0000000..cbd49ff
    - +  
     1from twisted.python.log import err
     2from twisted.web.client import Agent
     3from twisted.internet import task
     4from twisted.internet.ssl import ClientContextFactory
     5
     6class WebClientContextFactory(ClientContextFactory):
     7    def getContext(self, hostname, port):
     8        return ClientContextFactory.getContext(self)
     9
     10def display(response):
     11    print "Received response"
     12    print response
     13
     14def main(reactor):
     15    contextFactory = WebClientContextFactory()
     16    agent = Agent(reactor, contextFactory)
     17
     18    d = agent.request("GET", "https://example.com/")
     19    d.addCallbacks(display, err)
     20
     21    return d
     22
     23if __name__ == "__main__":
     24    task.react(main)
     25 No newline at end of file
  • doc/web/howto/listings/client/response.py

    diff --git doc/web/howto/listings/client/response.py doc/web/howto/listings/client/response.py
    index 6b3547c..5a4fb90 100644
     
    11from pprint import pformat
    22
    3 from twisted.internet import reactor
     3from twisted.internet import task
    44from twisted.internet.defer import Deferred
    55from twisted.internet.protocol import Protocol
    66from twisted.web.client import Agent
    class BeginningPrinter(Protocol): 
    2222        print 'Finished receiving body:', reason.getErrorMessage()
    2323        self.finished.callback(None)
    2424
    25 agent = Agent(reactor)
    26 d = agent.request(
    27     'GET',
    28     'http://example.com/',
    29     Headers({'User-Agent': ['Twisted Web Client Example']}),
    30     None)
    31 
    32 def cbRequest(response):
    33     print 'Response version:', response.version
    34     print 'Response code:', response.code
    35     print 'Response phrase:', response.phrase
    36     print 'Response headers:'
    37     print pformat(list(response.headers.getAllRawHeaders()))
    38     finished = Deferred()
    39     response.deliverBody(BeginningPrinter(finished))
    40     return finished
    41 d.addCallback(cbRequest)
    42 
    43 def cbShutdown(ignored):
    44     reactor.stop()
    45 d.addBoth(cbShutdown)
    46 
    47 reactor.run()
     25def main(reactor):
     26    agent = Agent(reactor)
     27    d = agent.request(
     28        'GET',
     29        'http://example.com/',
     30        Headers({'User-Agent': ['Twisted Web Client Example']}),
     31        None)
     32
     33    def cbRequest(response):
     34        print 'Response version:', response.version
     35        print 'Response code:', response.code
     36        print 'Response phrase:', response.phrase
     37        print 'Response headers:'
     38        print pformat(list(response.headers.getAllRawHeaders()))
     39        finished = Deferred()
     40        response.deliverBody(BeginningPrinter(finished))
     41        return finished
     42    d.addCallback(cbRequest)
     43
     44    return d
     45
     46task.react(main)
  • doc/web/howto/listings/client/sendbody.py

    diff --git doc/web/howto/listings/client/sendbody.py doc/web/howto/listings/client/sendbody.py
    index 31cac8f..5c338ec 100644
     
    1 from twisted.internet import reactor
     1from twisted.internet import task
    22from twisted.web.client import Agent
    33from twisted.web.http_headers import Headers
    44
    55from stringprod import StringProducer
    66
    7 agent = Agent(reactor)
    8 body = StringProducer("hello, world")
    9 d = agent.request(
    10     'GET',
    11     'http://example.com/',
    12     Headers({'User-Agent': ['Twisted Web Client Example'],
    13              'Content-Type': ['text/x-greeting']}),
    14     body)
     7def main(reactor):
     8    agent = Agent(reactor)
     9    body = StringProducer("hello, world")
     10    d = agent.request(
     11        'GET',
     12        'http://example.com/',
     13        Headers({'User-Agent': ['Twisted Web Client Example'],
     14                 'Content-Type': ['text/x-greeting']}),
     15        body)
    1516
    16 def cbResponse(ignored):
    17     print 'Response received'
    18 d.addCallback(cbResponse)
     17    def cbResponse(ignored):
     18        print 'Response received'
     19    d.addCallback(cbResponse)
    1920
    20 def cbShutdown(ignored):
    21     reactor.stop()
    22 d.addBoth(cbShutdown)
     21    return d
    2322
    24 reactor.run()
     23task.react(main)
     24 No newline at end of file