-
Notifications
You must be signed in to change notification settings - Fork 0
/
httlib.py
47 lines (35 loc) · 1.09 KB
/
httlib.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File: httplib-example-1.py
import httplib
USER_AGENT = "httplib-example-1.py"
class Error:
# indicates an HTTP error
def __init__(self, url, errcode, errmsg, headers):
self.url = url
self.errcode = errcode
self.errmsg = errmsg
self.headers = headers
def __repr__(self):
return (
"<Error for %s: %s %s>" %
(self.url, self.errcode, self.errmsg)
)
class Server:
def __init__(self, host):
self.host = host
def fetch(self, path):
http = httplib.HTTP(self.host)
# write header
http.putrequest("GET", path)
http.putheader("User-Agent", USER_AGENT)
http.putheader("Host", self.host)
http.putheader("Accept", "*/*")
http.endheaders()
# get response
errcode, errmsg, headers = http.getreply()
if errcode != 200:
raise Error(errcode, errmsg, headers)
file = http.getfile()
return file.read()
if __name__ == "__main__":
server = Server("www.pythonware.com")
print server.fetch("/index.htm")