removed syncron httpclient
[enigma2-plugins.git] / lastfm / src / httpclient.py
1 from twisted.internet import reactor\r
2 from twisted.internet.protocol import ClientFactory,connectionDone\r
3 from twisted.web2.client.http import HTTPClientProtocol\r
4 \r
5 from twisted.internet import error \r
6 \r
7 import socket\r
8 \r
9 class myProtocol(HTTPClientProtocol):\r
10     path = "/"\r
11     method = "GET"\r
12     data=""\r
13     header=""\r
14     headerread=False\r
15     def __init__(self,hostname,path,method="GET"):\r
16         self.path=path\r
17         self.method=method\r
18         self.hostname=hostname\r
19         HTTPClientProtocol.__init__(self)\r
20         self.setRawMode()\r
21         \r
22     def rawDataReceived(self,line):\r
23         for l in line.split(self.delimiter): \r
24             if self.headerread:\r
25                 self.data +=l\r
26             else:\r
27                 if l == "":\r
28                     self.headerread=True\r
29                 else:\r
30                     self.header +=l\r
31                 \r
32     def connectionMade(self):\r
33         self.sendLine("%s %s HTTP/1.0"%(self.method,self.path))\r
34         self.sendLine("Host: %s"%self.hostname)\r
35         self.sendLine("User-Agent: enigma2 lastfm")\r
36         self.sendLine("")\r
37 \r
38         \r
39 class myClientFactory(ClientFactory):\r
40 \r
41     initialDelay = 20\r
42     maxDelay = 500\r
43     \r
44     def __init__(self,hostname,path,method="GET",callback=None,errorback=None):\r
45         self.hangup_ok = False\r
46         self.path=path\r
47         self.method=method\r
48         self.callback=callback\r
49         self.errorback=errorback\r
50         self.protocol = myProtocol(hostname,self.path,method=self.method)\r
51         \r
52     def startedConnecting(self, connector):\r
53         pass\r
54     def buildProtocol(self, addr):\r
55         return self.protocol\r
56 \r
57     def clientConnectionLost(self, connector, reason):\r
58         if not self.hangup_ok:\r
59             self.callback(self.protocol.data)\r
60     def clientConnectionFailed(self, connector, reason):\r
61         if self.errorback is not None:\r
62             self.errorback(reason.getErrorMessage())\r
63         ClientFactory.clientConnectionFailed(self, connector, reason)\r
64 \r
65 \r
66 class testConn:\r
67     def __init__(self,hostname,port,path,method="GET",callback=None,errorback=None):\r
68         f = myClientFactory(hostname,path,method,callback,errorback)\r
69         try:\r
70             hostname = socket.gethostbyname(hostname)\r
71         except socket.error:\r
72             msg = "address %r not found" % (hostname,)\r
73             if errorback is not None:\r
74                 errorback(msg)\r
75         \r
76         reactor.connectTCP(hostname, port, f)\r