Simple HTTP client import socket class SimpleHTTP def __init__ self ho

 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
"""Simple HTTP client"""
import socket
class SimpleHTTP:
def __init__(self, host, port=None):
self.sock = None
self._set_hostport(host, port)
def _set_hostport(self, host, port):
""" Set up host and port. """
if port is None:
i = host.rfind(':')
if i > 0:
try:
port = int(host[i+1:])
except ValueError:
raise InvalidURL("Bad port: '%s'" % host[i+1:])
host = host[:i]
else:
port = 80 # default HTTP port
self.host = host
self.port = port
def connect(self):
""" Connecting to host and port specified in __init__. """
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
except socket.error, msg:
raise socket.error, msg
def close(self):
"""Close the connection to the HTTP server."""
if self.sock:
self.sock.close()
self.sock = None
p = SimpleHTTP('ya.ru')
print p.host
p.connect # почему не робит?