add asycron httpclient
[enigma2-plugins.git] / lastfm / src / httpclient.py
1 from twisted.python.log import startLogging\r
2 #startLogging(open("/tmp/twisted.log",'w'))\r
3 \r
4 from twisted.internet import reactor\r
5 from twisted.internet.protocol import ClientFactory,connectionDone\r
6 from twisted.web2.client.http import HTTPClientProtocol\r
7 \r
8 from twisted.internet import error \r
9 \r
10 import socket\r
11 \r
12 class myProtocol(HTTPClientProtocol):\r
13     path = "/"\r
14     method = "GET"\r
15     data=""\r
16     header=""\r
17     headerread=False\r
18     def __init__(self,hostname,path,method="GET"):\r
19         self.path=path\r
20         self.method=method\r
21         self.hostname=hostname\r
22         HTTPClientProtocol.__init__(self)\r
23         self.setRawMode()\r
24         \r
25     def rawDataReceived(self,line):\r
26         for l in line.split(self.delimiter): \r
27             if self.headerread:\r
28                 #print "BODY",l\r
29                 self.data +=l\r
30             else:\r
31                 if l == "":\r
32                     #print "END HEADER",l\r
33                     self.headerread=True\r
34                 else:\r
35                     #print "HEADER",l\r
36                     self.header +=l\r
37                 \r
38     def connectionMade(self):\r
39         self.sendLine("%s %s HTTP/1.0"%(self.method,self.path))\r
40         self.sendLine("Host: %s"%self.hostname)\r
41         self.sendLine("User-Agent: enigma2 lastfm")\r
42         self.sendLine("")\r
43 \r
44         \r
45 class myClientFactory(ClientFactory):\r
46 \r
47     initialDelay = 20\r
48     maxDelay = 500\r
49     \r
50     def __init__(self,hostname,path,method="GET",callback=None):\r
51         self.hangup_ok = False\r
52         self.path=path\r
53         self.method=method\r
54         self.callback=callback\r
55         self.protocol = myProtocol(hostname,self.path,method=self.method)\r
56         \r
57     def startedConnecting(self, connector):\r
58         pass\r
59     def buildProtocol(self, addr):\r
60         return self.protocol\r
61 \r
62     def clientConnectionLost(self, connector, reason):\r
63         if not self.hangup_ok:\r
64             self.callback(self.protocol.data)\r
65     def clientConnectionFailed(self, connector, reason):\r
66         print "Connection to host failed! (%s)" % reason.getErrorMessage()\r
67         ClientFactory.clientConnectionFailed(self, connector, reason)\r
68 \r
69 \r
70 class testConn:\r
71     def __init__(self,hostname,port,path,method="GET",callback=None,errback=None):\r
72         print hostname\r
73         f = myClientFactory(hostname,path,method,callback)\r
74         try:\r
75             hostname = socket.gethostbyname(hostname)\r
76         except socket.error:\r
77             msg = "address %r not found" % (hostname,)\r
78             if errback is not None:\r
79                 errback(msg)\r
80         \r
81         reactor.connectTCP(hostname, port, f)\r
82 #########\r
83 \r
84 import base64\r
85 \r
86 import socket\r
87 import string\r
88 \r
89 True = 1\r
90 False = 0\r
91 \r
92 class httpclientDISABLED:\r
93 \r
94     def __init__(self, host, port):\r
95         self.host = host\r
96         self.port = port\r
97         self.status = None\r
98         self.headers = None\r
99         self.response = None\r
100 \r
101     def readline(self, s):\r
102         res = ""\r
103         while True:\r
104             try:\r
105                 c = s.recv(1)\r
106             except:\r
107                 break\r
108             res = res + c\r
109             if c == '\n':\r
110                 break\r
111             if not c:\r
112                 break\r
113         #print res\r
114         return res\r
115 \r
116     def req(self, url):\r
117         try:\r
118             s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
119             s.settimeout(3)\r
120 #        if config.useproxy:\r
121 #            s.connect((config.proxyhost, config.proxyport))\r
122 #            s.send("GET http://" + self.host + ":" + str(self.port) + url + " HTTP/1.0\r\n")\r
123 #            if config.proxyuser != "":\r
124 #                s.send("Proxy-Authorization: Basic " + base64.b64encode(config.proxyuser + ":" + config.proxypass) + "\r\n")\r
125 #        else:\r
126 #            print "reg: ",self.host, self.port,url\r
127             s.connect((self.host, self.port))\r
128             s.send("GET " + url + " HTTP/1.0\r\n")\r
129             s.send("Host: " + self.host + "\r\n")\r
130             s.send("\r\n")\r
131 \r
132             line = self.readline(s)\r
133             #print line\r
134             self.status = string.rstrip(line)\r
135             \r
136             self.headers = {}\r
137             while True:\r
138                 line = self.readline(s)\r
139                 if not line:\r
140                     break\r
141                 if line == "\r\n":\r
142                     break\r
143                 tmp = string.split(line, ": ")\r
144                 try:\r
145                   self.headers[tmp[0]] = string.rstrip(tmp[1])\r
146                 except:\r
147                   print "BUG"\r
148                   print "self.headers[tmp[0]] = string.rstrip(tmp[1]) has no tmp[1]"\r
149                   print line\r
150                   print tmp              \r
151             self.response = ""\r
152             while True:\r
153                 line = self.readline(s)\r
154                 if not line:\r
155                     break\r
156                 self.response = self.response + line\r
157             s.close()\r
158         except socket.error,e:\r
159             print e\r
160             self.response = ""\r
161             return False,e\r