libsaria/web.py

This file contains a url class designed to make it easy to build up a
url and then place a python urllib2 request.
This commit is contained in:
Bryan Schumaker 2010-10-26 22:40:00 -04:00
parent e3327093ad
commit 657d016017
1 changed files with 33 additions and 0 deletions

33
libsaria/web.py Normal file
View File

@ -0,0 +1,33 @@
# Bryan Schumaker (10/24/2010)
import libsaria
import urllib2
import string
HEADER = libsaria.__vers__
vals = [ ('%','25'), (' ','20'), ('&','26'), ('<','3C'), ('>','3E'),
('"','22'), ('#','23'), ('$','24'), ('\'','27'), ('+','2B'),
(',','2C'), ('/','2F'), (':','3A'), ('[','5B'), (']','5D')]
class Url:
def __init__(self, url):
self.url = url
def __setitem__(self, key, value):
value = escape(value)
self.url += "&" + key + "=" + value
def open(self):
#print self.url
req = urllib2.Request(self.url)
req.add_header('User-Agent', HEADER)
return urllib2.urlopen(req)
def escape(string):
for l in vals:
string = string.replace(l[0], "%s%s" % ('%', l[1]))
return string