Ticket #886: httpclient.py.https_and_host.patch

File httpclient.py.https_and_host.patch, 1.3 KB (added by ivank, 8 years ago)

https support for httpclient.py example. Also, send non-default ports as part of Host header.

  • doc/web/examples/httpclient.py

    old new  
    3535def main(reactor, url):
    3636    cc = ClientCreator(reactor, HTTP11ClientProtocol)
    3737    scheme, host, port, path = _parse(url)
    38     if scheme != 'http':
     38    if scheme == 'http':
     39        d = cc.connectTCP(host, port)
     40    elif scheme == 'https':
     41        from twisted.internet.ssl import ClientContextFactory
     42        contextFactory = ClientContextFactory()
     43        d = cc.connectSSL(host, port, contextFactory)
     44    else:
    3945        raise SystemExit("Unsupported scheme: %r" % (scheme,))
    40     d = cc.connectTCP(host, port)
    4146    def cbConnected(proto):
     47        # XXX This port information is redundant with the numbers in t.w.client._parse
     48        defaultPorts = {'http': 80, 'https': 443}
     49        host_header = host
     50        if defaultPorts[scheme] != port:
     51            host_header += ':%d' % port
    4252        return proto.request(Request(
    4353                'GET', path,
    44                 Headers({'host': [host], 'user-agent': ['Twisted/%s (httpclient.py)' % (version.short(),)]}),
     54                Headers({'host': [host_header], 'user-agent': ['Twisted/%s (httpclient.py)' % (version.short(),)]}),
    4555                None))
    4656    d.addCallback(cbConnected)
    4757    def cbResponse(response):