Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from grierj/master

Add User-Agent header to seco.range requests.
  • Loading branch information...
commit 7d3d1a316e1907f5552012f3fc5beec66e46c659 2 parents 37fdd09 + 8019dc7
@cuzelac cuzelac authored
Showing with 19 additions and 2 deletions.
  1. +19 −2 python_seco_range/source/seco/range.py
View
21 python_seco_range/source/seco/range.py
@@ -4,7 +4,11 @@
"""
import urllib2
+import socket
import sys
+import getpass
+
+__version__ = '1.0'
class RangeException(Exception):
def __init__(self, value):
@@ -13,8 +17,10 @@ def __str__(self):
return repr(self.value)
class Range(object):
- def __init__(self, host):
+ def __init__(self, host, user_agent=None):
self.host = host
+ self.headers = {}
+ self.headers['User-Agent'] = self.get_user_agent(user_agent)
def expand(self, expr, ret_list=True):
if isinstance(expr, list):
@@ -23,9 +29,10 @@ def expand(self, expr, ret_list=True):
url = 'http://%s/range/list?%s' % (self.host, urllib2.quote(expr))
else:
url = 'http://%s/range/expand?%s' % (self.host, urllib2.quote(expr))
+ range_req = urllib2.Request(url, None, self.headers)
req = None
try:
- req = urllib2.urlopen(url)
+ req = urllib2.urlopen(range_req)
except urllib2.URLError, e:
raise RangeException(e)
code = req.getcode()
@@ -47,6 +54,16 @@ def expand(self, expr, ret_list=True):
def collapse(self, expr):
return self.expand(expr, ret_list=False)
+ def get_user_agent(self, provided_agent):
+ """
+ Build a verbose User-Agent for sending to the range server.
+ Terribly useful if you ever have to track down crappy clients.
+ """
+ myhost = socket.gethostname()
+ me = getpass.getuser()
+ myscript = provided_agent or sys.argv[0] or 'seco.range'
+ return '{0}/{1} ({2}; {3})'.format(myscript, __version__, me, myhost)
+
if __name__ == '__main__':
try:
r = Range("localhost:80")
Please sign in to comment.
Something went wrong with that request. Please try again.