]> review.fuel-infra Code Review - packages/trusty/python-eventlet.git/blob - examples/twisted/twisted_client.py
Add python-eventlet package to MOS 9.0 repository
[packages/trusty/python-eventlet.git] / examples / twisted / twisted_client.py
1 """Example for GreenTransport and GreenClientCreator.
2
3 In this example reactor is started implicitly upon the first
4 use of a blocking function.
5 """
6 from twisted.internet import ssl
7 from twisted.internet.error import ConnectionClosed
8 from eventlet.twistedutil.protocol import GreenClientCreator
9 from eventlet.twistedutil.protocols.basic import LineOnlyReceiverTransport
10 from twisted.internet import reactor
11
12 # read from TCP connection
13 conn = GreenClientCreator(reactor).connectTCP('www.google.com', 80)
14 conn.write('GET / HTTP/1.0\r\n\r\n')
15 conn.loseWriteConnection()
16 print(conn.read())
17
18 # read from SSL connection line by line
19 conn = GreenClientCreator(reactor, LineOnlyReceiverTransport).connectSSL('sf.net', 443, ssl.ClientContextFactory())
20 conn.write('GET / HTTP/1.0\r\n\r\n')
21 try:
22     for num, line in enumerate(conn):
23         print('%3s %r' % (num, line))
24 except ConnectionClosed as ex:
25     print(ex)
26