3
3
This version adds a Request object.
4
4
"""
5
5
6
+ import asyncio
7
+ from http .client import BadStatusLine
6
8
import sys
7
9
import urllib .parse
8
- from http .client import BadStatusLine
9
-
10
- from asyncio import *
11
10
12
11
13
12
class Request :
@@ -34,13 +33,13 @@ def __init__(self, url, verbose=True):
34
33
self .reader = None
35
34
self .writer = None
36
35
37
- @coroutine
36
+ @asyncio . coroutine
38
37
def connect (self ):
39
38
if self .verbose :
40
39
print ('* Connecting to %s:%s using %s' %
41
40
(self .hostname , self .port , 'ssl' if self .ssl else 'tcp' ),
42
41
file = sys .stderr )
43
- self .reader , self .writer = yield from open_connection (self .hostname ,
42
+ self .reader , self .writer = yield from asyncio . open_connection (self .hostname ,
44
43
self .port ,
45
44
ssl = self .ssl )
46
45
if self .verbose :
@@ -51,20 +50,22 @@ def connect(self):
51
50
def putline (self , line ):
52
51
self .writer .write (line .encode ('latin-1' ) + b'\r \n ' )
53
52
54
- @coroutine
53
+ @asyncio . coroutine
55
54
def send_request (self ):
56
55
request = '%s %s %s' % (self .method , self .full_path , self .http_version )
57
- if self .verbose : print ('>' , request , file = sys .stderr )
56
+ if self .verbose :
57
+ print ('>' , request , file = sys .stderr )
58
58
self .putline (request )
59
59
if 'host' not in {key .lower () for key , _ in self .headers }:
60
60
self .headers .insert (0 , ('Host' , self .netloc ))
61
61
for key , value in self .headers :
62
62
line = '%s: %s' % (key , value )
63
- if self .verbose : print ('>' , line , file = sys .stderr )
63
+ if self .verbose :
64
+ print ('>' , line , file = sys .stderr )
64
65
self .putline (line )
65
66
self .putline ('' )
66
67
67
- @coroutine
68
+ @asyncio . coroutine
68
69
def get_response (self ):
69
70
response = Response (self .reader , self .verbose )
70
71
yield from response .read_headers ()
@@ -81,14 +82,15 @@ def __init__(self, reader, verbose=True):
81
82
self .reason = None # 'Ok'
82
83
self .headers = [] # [('Content-Type', 'text/html')]
83
84
84
- @coroutine
85
+ @asyncio . coroutine
85
86
def getline (self ):
86
87
return (yield from self .reader .readline ()).decode ('latin-1' ).rstrip ()
87
88
88
- @coroutine
89
+ @asyncio . coroutine
89
90
def read_headers (self ):
90
91
status_line = yield from self .getline ()
91
- if self .verbose : print ('<' , status_line , file = sys .stderr )
92
+ if self .verbose :
93
+ print ('<' , status_line , file = sys .stderr )
92
94
status_parts = status_line .split (None , 2 )
93
95
if len (status_parts ) != 3 :
94
96
raise BadStatusLine (status_line )
@@ -98,13 +100,15 @@ def read_headers(self):
98
100
header_line = yield from self .getline ()
99
101
if not header_line :
100
102
break
101
- if self .verbose : print ('<' , header_line , file = sys .stderr )
103
+ if self .verbose :
104
+ print ('<' , header_line , file = sys .stderr )
102
105
# TODO: Continuation lines.
103
106
key , value = header_line .split (':' , 1 )
104
107
self .headers .append ((key , value .strip ()))
105
- if self .verbose : print (file = sys .stderr )
108
+ if self .verbose :
109
+ print (file = sys .stderr )
106
110
107
- @coroutine
111
+ @asyncio . coroutine
108
112
def read (self ):
109
113
nbytes = None
110
114
for key , value in self .headers :
@@ -118,7 +122,7 @@ def read(self):
118
122
return body
119
123
120
124
121
- @coroutine
125
+ @asyncio . coroutine
122
126
def fetch (url , verbose = True ):
123
127
request = Request (url , verbose )
124
128
yield from request .connect ()
@@ -129,7 +133,7 @@ def fetch(url, verbose=True):
129
133
130
134
131
135
def main ():
132
- loop = get_event_loop ()
136
+ loop = asyncio . get_event_loop ()
133
137
try :
134
138
body = loop .run_until_complete (fetch (sys .argv [1 ], '-v' in sys .argv ))
135
139
finally :
0 commit comments