diff --git a/pubsubhubbub_publish.py b/pubsubhubbub_publish.py index 9ae6e66..4296c98 100644 --- a/pubsubhubbub_publish.py +++ b/pubsubhubbub_publish.py @@ -34,8 +34,24 @@ __author__ = 'bslatkin@gmail.com (Brett Slatkin)' -import urllib -import urllib2 +import codecs +# making compatatible variables between py2 and py3. +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode +try: + from urllib2 import urlopen, HTTPError +except ImportError: + from urllib.request import urlopen, HTTPError +try: + basestring +except NameError: + basestring = (str,) +try: + xrange +except NameError: + xrange = range class PublishError(Exception): @@ -64,13 +80,11 @@ def publish(hub, *urls): for i in xrange(0, len(urls), URL_BATCH_SIZE): chunk = urls[i:i+URL_BATCH_SIZE] - data = urllib.urlencode( + data = urlencode( {'hub.url': chunk, 'hub.mode': 'publish'}, doseq=True) try: - response = urllib2.urlopen(hub, data) - except (IOError, urllib2.HTTPError), e: - if hasattr(e, 'code') and e.code == 204: - continue + response = urlopen(hub, codecs.encode(data)) + except (IOError, HTTPError) as e: error = '' if hasattr(e, 'read'): error = e.read() diff --git a/pubsubhubbub_publish_test.py b/pubsubhubbub_publish_test.py index 09a185b..d4994b5 100755 --- a/pubsubhubbub_publish_test.py +++ b/pubsubhubbub_publish_test.py @@ -19,21 +19,28 @@ __author__ = 'bslatkin@gmail.com (Brett Slatkin)' -import BaseHTTPServer -import urllib import unittest import threading +import codecs +# making compatatible variables between py2 and py3. +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode +try: + from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +except ImportError: + from http.server import BaseHTTPRequestHandler, HTTPServer import pubsubhubbub_publish - REQUESTS = 0 -class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class RequestHandler(BaseHTTPRequestHandler): def do_POST(self): global REQUESTS - print 'Accessed', self.path + print('Accessed', self.path) REQUESTS += 1 length = int(self.headers.get('content-length', 0)) @@ -42,26 +49,29 @@ def do_POST(self): body = self.rfile.read(length) if self.path == '/single': - if body != urllib.urlencode( - {'hub.url': 'http://example.com/feed', 'hub.mode': 'publish'}): + if body != codecs.encode(urlencode( + {'hub.url': 'http://example.com/feed', 'hub.mode': 'publish'})): self.send_error(500) self.wfile.write('Bad body. Found:') self.wfile.write(body) else: self.send_response(204) + self.end_headers() elif self.path == '/multiple': - if body != urllib.urlencode( + if body != codecs.encode(urlencode( {'hub.url': ['http://example.com/feed', 'http://example.com/feed2', 'http://example.com/feed3'], - 'hub.mode': 'publish'}, doseq=True): + 'hub.mode': 'publish'}, doseq=True)): self.send_error(500) self.wfile.write('Bad body. Found:') self.wfile.write(body) else: self.send_response(204) + self.end_headers() elif self.path == '/batch': self.send_response(204) + self.end_headers() elif self.path == '/fail': self.send_error(400) self.wfile.write('bad argument') @@ -74,7 +84,7 @@ class PublishTest(unittest.TestCase): def setUp(self): global REQUESTS REQUESTS = 0 - self.server = BaseHTTPServer.HTTPServer(('', 0), RequestHandler) + self.server = HTTPServer(('', 0), RequestHandler) t = threading.Thread(target=self.server.serve_forever) t.setDaemon(True) t.start()