Ticket #886: httpclient.py.https_and_host.patch
| File httpclient.py.https_and_host.patch, 1.3 KB (added by ivank, 4 years ago) |
|---|
-
doc/web/examples/httpclient.py
old new 35 35 def main(reactor, url): 36 36 cc = ClientCreator(reactor, HTTP11ClientProtocol) 37 37 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: 39 45 raise SystemExit("Unsupported scheme: %r" % (scheme,)) 40 d = cc.connectTCP(host, port)41 46 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 42 52 return proto.request(Request( 43 53 '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(),)]}), 45 55 None)) 46 56 d.addCallback(cbConnected) 47 57 def cbResponse(response):
