Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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__ = '[email protected] (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__ = '[email protected] (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