import asyncio import ssl if True: # madis HOST = 'madis-data.ncep.noaa.gov' PORT = 443 PATH = '/madisPublic1/data/archive/2001/07/01/LDAD/mesonet/netCDF/20010701_0800.gz' else: # localhost HOST = 'localhost' PORT = 443 PATH = '/20010701_0800.gz' MSG = 'GET {path} HTTP/1.1\r\nIf-Modified-Since: Tue, 31 Jan 2017 05:45:10 GMT\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nHost: madis-data.ncep.noaa.gov\r\nUser-Agent: Python/3.5 aiohttp/1.3.0a0\r\nContent-Length: 0\r\n\r\n'.format(path=PATH) class HttpClient(asyncio.Protocol): def connection_made(self, transport): self.transport = transport self.transport.write(MSG.encode('ascii')) print('data sent:', MSG) def data_received(self, data): print('data received:', data) self.transport.close() # asyncio.get_event_loop().call_later(1.0, ) def eof_received(self): pass def connection_lost(self, exc): print('connection lost:', exc) asyncio.get_event_loop().stop() async def main(): loop = asyncio.get_event_loop() ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ssl_context.set_default_verify_paths() ssl_context.load_cert_chain('/tmp/cert', '/tmp/key') return await loop.create_connection(HttpClient, HOST, PORT, ssl=ssl_context) if __name__ == '__main__': _loop = asyncio.get_event_loop() _loop.run_until_complete(main()) _loop.run_forever()