Skip to content

Commit

Permalink
rewrote to be able to run in python3
Browse files Browse the repository at this point in the history
  • Loading branch information
righ authored and righ committed Mar 18, 2018
1 parent 0856f91 commit 0602c1c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
28 changes: 21 additions & 7 deletions pubsubhubbub_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 20 additions & 10 deletions pubsubhubbub_publish_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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')
Expand All @@ -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()
Expand Down

0 comments on commit 0602c1c

Please sign in to comment.