1 from twisted.python.log import startLogging
\r
2 #startLogging(open("/tmp/twisted.log",'w'))
\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
8 from twisted.internet import error
\r
12 class myProtocol(HTTPClientProtocol):
\r
18 def __init__(self,hostname,path,method="GET"):
\r
21 self.hostname=hostname
\r
22 HTTPClientProtocol.__init__(self)
\r
25 def rawDataReceived(self,line):
\r
26 for l in line.split(self.delimiter):
\r
32 #print "END HEADER",l
\r
33 self.headerread=True
\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
45 class myClientFactory(ClientFactory):
\r
50 def __init__(self,hostname,path,method="GET",callback=None):
\r
51 self.hangup_ok = False
\r
54 self.callback=callback
\r
55 self.protocol = myProtocol(hostname,self.path,method=self.method)
\r
57 def startedConnecting(self, connector):
\r
59 def buildProtocol(self, addr):
\r
60 return self.protocol
\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
71 def __init__(self,hostname,port,path,method="GET",callback=None,errback=None):
\r
73 f = myClientFactory(hostname,path,method,callback)
\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
81 reactor.connectTCP(hostname, port, f)
\r
92 class httpclientDISABLED:
\r
94 def __init__(self, host, port):
\r
99 self.response = None
\r
101 def readline(self, s):
\r
116 def req(self, url):
\r
118 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
\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
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
132 line = self.readline(s)
\r
134 self.status = string.rstrip(line)
\r
138 line = self.readline(s)
\r
143 tmp = string.split(line, ": ")
\r
145 self.headers[tmp[0]] = string.rstrip(tmp[1])
\r
148 print "self.headers[tmp[0]] = string.rstrip(tmp[1]) has no tmp[1]"
\r
153 line = self.readline(s)
\r
156 self.response = self.response + line
\r
158 except socket.error,e:
\r