initial version
[enigma2-plugins.git] / lastfm / src / httpclient.py
1
2 import base64
3
4 import socket
5 import string
6
7 True = 1
8 False = 0
9
10 class httpclient:
11
12     def __init__(self, host, port):
13         self.host = host
14         self.port = port
15         self.status = None
16         self.headers = None
17         self.response = None
18
19     def readline(self, s):
20         res = ""
21         while True:
22             try:
23                 c = s.recv(1)
24             except:
25                 break
26             res = res + c
27             if c == '\n':
28                 break
29             if not c:
30                 break
31         #print res
32         return res
33
34     def req(self, url):
35         try:
36             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37             s.settimeout(3)
38 #        if config.useproxy:
39 #            s.connect((config.proxyhost, config.proxyport))
40 #            s.send("GET http://" + self.host + ":" + str(self.port) + url + " HTTP/1.0\r\n")
41 #            if config.proxyuser != "":
42 #                s.send("Proxy-Authorization: Basic " + base64.b64encode(config.proxyuser + ":" + config.proxypass) + "\r\n")
43 #        else:
44 #            print "reg: ",self.host, self.port,url
45             s.connect((self.host, self.port))
46             s.send("GET " + url + " HTTP/1.0\r\n")
47             s.send("Host: " + self.host + "\r\n")
48             s.send("\r\n")
49
50             line = self.readline(s)
51             #print line
52             self.status = string.rstrip(line)
53             
54             self.headers = {}
55             while True:
56                 line = self.readline(s)
57                 if not line:
58                     break
59                 if line == "\r\n":
60                     break
61                 tmp = string.split(line, ": ")
62                 try:
63                   self.headers[tmp[0]] = string.rstrip(tmp[1])
64                 except:
65                   print "BUG"
66                   print "self.headers[tmp[0]] = string.rstrip(tmp[1]) has no tmp[1]"
67                   print line
68                   print tmp              
69             self.response = ""
70             while True:
71                 line = self.readline(s)
72                 if not line:
73                     break
74                 self.response = self.response + line
75             s.close()
76         except socket.error,e:
77             print e
78             self.response = ""
79             return False,e