]> git.mxchange.org Git - quix0rs-apt-p2p.git/blobdiff - apt_dht/PeerManager.py
Upgrade pysqlite from 1.0 to 2.x (now uses sqlite3).
[quix0rs-apt-p2p.git] / apt_dht / PeerManager.py
index 115edad8cc694931691b33b618a3f6baf1148bd7..25bd4f5959e1015aa26848ba18805e7f6741aa4e 100644 (file)
@@ -1,9 +1,12 @@
 
 from random import choice
+from urlparse import urlparse, urlunparse
 
 from twisted.internet import reactor, defer
+from twisted.python import log
 from twisted.trial import unittest
 from twisted.web2 import stream as stream_mod
+from twisted.web2.http import splitHostPort
 
 from HTTPDownloader import HTTPClientManager
 
@@ -11,13 +14,19 @@ class PeerManager:
     def __init__(self):
         self.clients = {}
         
-    def get(self, location_list, method="GET", modtime=None):
+    def get(self, locations, method="GET", modtime=None):
         """Download from a list of peers.
         
-        @type location_list: C{list} of (C{string}, C{int}, C{string})
-        @var location_list: a list of the locations where the file can be found
+        @type locations: C{list} of C{string}
+        @var locations: a list of the locations where the file can be found
         """
-        host, port, path = choice(location_list)
+        url = choice(locations)
+        log.msg('Downloading %s' % url)
+        parsed = urlparse(url)
+        assert(parsed[0] == "http", "Only HTTP is supported, not '%s'" % parsed[0])
+        host, port = splitHostPort(parsed[0], parsed[1])
+        path = urlunparse(('', '') + parsed[2:])
+
         return self.getPeer(host, port, path, method, modtime)
         
     def getPeer(self, host, port, path, method="GET", modtime=None):
@@ -54,7 +63,7 @@ class TestPeerManager(unittest.TestCase):
         self.timeout = 10
         
         host = 'www.camrdale.org'
-        d = self.manager.get([(host, 80, '/robots.txt')])
+        d = self.manager.get(['http://' + host + '/robots.txt'])
         d.addCallback(self.gotResp, 1, 309)
         return d
         
@@ -63,7 +72,7 @@ class TestPeerManager(unittest.TestCase):
         self.timeout = 10
         
         host = 'www.camrdale.org'
-        d = self.manager.get([(host, 80, '/robots.txt')], "HEAD")
+        d = self.manager.get(['http://' + host + '/robots.txt'], "HEAD")
         d.addCallback(self.gotResp, 1, 0)
         return d
         
@@ -73,7 +82,7 @@ class TestPeerManager(unittest.TestCase):
         lastDefer = defer.Deferred()
         
         def newRequest(host, path, num, expect, last=False):
-            d = self.manager.get([(host, 80, path)])
+            d = self.manager.get(['http://' + host + ':' + str(80) + path])
             d.addCallback(self.gotResp, num, expect)
             if last:
                 d.addBoth(lastDefer.callback)