removed syncron httpclient
authorRico Schulte <ricoschulte@users.schwerkraft.elitedvb.net>
Sun, 8 Apr 2007 14:19:34 +0000 (14:19 +0000)
committerRico Schulte <ricoschulte@users.schwerkraft.elitedvb.net>
Sun, 8 Apr 2007 14:19:34 +0000 (14:19 +0000)
lastfm/src/httpclient.py

index 4c2da16..6cce45e 100755 (executable)
@@ -1,6 +1,3 @@
-from twisted.python.log import startLogging\r
-#startLogging(open("/tmp/twisted.log",'w'))\r
-\r
 from twisted.internet import reactor\r
 from twisted.internet.protocol import ClientFactory,connectionDone\r
 from twisted.web2.client.http import HTTPClientProtocol\r
@@ -25,14 +22,11 @@ class myProtocol(HTTPClientProtocol):
     def rawDataReceived(self,line):\r
         for l in line.split(self.delimiter): \r
             if self.headerread:\r
-                #print "BODY",l\r
                 self.data +=l\r
             else:\r
                 if l == "":\r
-                    #print "END HEADER",l\r
                     self.headerread=True\r
                 else:\r
-                    #print "HEADER",l\r
                     self.header +=l\r
                 \r
     def connectionMade(self):\r
@@ -47,11 +41,12 @@ class myClientFactory(ClientFactory):
     initialDelay = 20\r
     maxDelay = 500\r
     \r
-    def __init__(self,hostname,path,method="GET",callback=None):\r
+    def __init__(self,hostname,path,method="GET",callback=None,errorback=None):\r
         self.hangup_ok = False\r
         self.path=path\r
         self.method=method\r
         self.callback=callback\r
+        self.errorback=errorback\r
         self.protocol = myProtocol(hostname,self.path,method=self.method)\r
         \r
     def startedConnecting(self, connector):\r
@@ -63,99 +58,19 @@ class myClientFactory(ClientFactory):
         if not self.hangup_ok:\r
             self.callback(self.protocol.data)\r
     def clientConnectionFailed(self, connector, reason):\r
-        print "Connection to host failed! (%s)" % reason.getErrorMessage()\r
+        if self.errorback is not None:\r
+            self.errorback(reason.getErrorMessage())\r
         ClientFactory.clientConnectionFailed(self, connector, reason)\r
 \r
 \r
 class testConn:\r
-    def __init__(self,hostname,port,path,method="GET",callback=None,errback=None):\r
-        print hostname\r
-        f = myClientFactory(hostname,path,method,callback)\r
+    def __init__(self,hostname,port,path,method="GET",callback=None,errorback=None):\r
+        f = myClientFactory(hostname,path,method,callback,errorback)\r
         try:\r
             hostname = socket.gethostbyname(hostname)\r
         except socket.error:\r
             msg = "address %r not found" % (hostname,)\r
-            if errback is not None:\r
-                errback(msg)\r
+            if errorback is not None:\r
+                errorback(msg)\r
         \r
         reactor.connectTCP(hostname, port, f)\r
-#########\r
-\r
-import base64\r
-\r
-import socket\r
-import string\r
-\r
-True = 1\r
-False = 0\r
-\r
-class httpclientDISABLED:\r
-\r
-    def __init__(self, host, port):\r
-        self.host = host\r
-        self.port = port\r
-        self.status = None\r
-        self.headers = None\r
-        self.response = None\r
-\r
-    def readline(self, s):\r
-        res = ""\r
-        while True:\r
-            try:\r
-                c = s.recv(1)\r
-            except:\r
-                break\r
-            res = res + c\r
-            if c == '\n':\r
-                break\r
-            if not c:\r
-                break\r
-        #print res\r
-        return res\r
-\r
-    def req(self, url):\r
-        try:\r
-            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\r
-            s.settimeout(3)\r
-#        if config.useproxy:\r
-#            s.connect((config.proxyhost, config.proxyport))\r
-#            s.send("GET http://" + self.host + ":" + str(self.port) + url + " HTTP/1.0\r\n")\r
-#            if config.proxyuser != "":\r
-#                s.send("Proxy-Authorization: Basic " + base64.b64encode(config.proxyuser + ":" + config.proxypass) + "\r\n")\r
-#        else:\r
-#            print "reg: ",self.host, self.port,url\r
-            s.connect((self.host, self.port))\r
-            s.send("GET " + url + " HTTP/1.0\r\n")\r
-            s.send("Host: " + self.host + "\r\n")\r
-            s.send("\r\n")\r
-\r
-            line = self.readline(s)\r
-            #print line\r
-            self.status = string.rstrip(line)\r
-            \r
-            self.headers = {}\r
-            while True:\r
-                line = self.readline(s)\r
-                if not line:\r
-                    break\r
-                if line == "\r\n":\r
-                    break\r
-                tmp = string.split(line, ": ")\r
-                try:\r
-                  self.headers[tmp[0]] = string.rstrip(tmp[1])\r
-                except:\r
-                  print "BUG"\r
-                  print "self.headers[tmp[0]] = string.rstrip(tmp[1]) has no tmp[1]"\r
-                  print line\r
-                  print tmp              \r
-            self.response = ""\r
-            while True:\r
-                line = self.readline(s)\r
-                if not line:\r
-                    break\r
-                self.response = self.response + line\r
-            s.close()\r
-        except socket.error,e:\r
-            print e\r
-            self.response = ""\r
-            return False,e\r