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:
parent
e3327093ad
commit
657d016017
|
@ -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
|
||||
|
Loading…
Reference in New Issue