forked from AGProjects/python3-eventlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwisted_client.py
More file actions
30 lines (24 loc) · 1.02 KB
/
twisted_client.py
File metadata and controls
30 lines (24 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""Example for GreenTransport and GreenClientCreator.
In this example reactor is started implicitly upon the first
use of a blocking function.
"""
from twisted.internet import ssl
from twisted.internet.error import ConnectionClosed
from eventlib.twistedutil.protocol import GreenClientCreator
from eventlib.twistedutil.protocols.basic import LineOnlyReceiverTransport
from twisted.internet import reactor
print("\n\nRead from TCP connection\n\n")
# read from TCP connection
conn = GreenClientCreator(reactor).connectTCP('www.google.com', 80)
conn.write('GET /not_found HTTP/1.0\r\n\r\n')
conn.loseWriteConnection()
print(conn.read().decode('utf-8'))
print("\n\nRead from SSL connection line by line\n\n")
# read from SSL connection line by line
conn = GreenClientCreator(reactor, LineOnlyReceiverTransport).connectSSL('ssltest.com', 443, ssl.ClientContextFactory())
conn.write('GET /not_found HTTP/1.0\r\n\r\n')
try:
for num, line in enumerate(conn):
print('%3s %r' % (num, line))
except ConnectionClosed as ex:
print(ex)