def test_url_encoding(self):
# Try some URL encodings of the PATHs.
# (the behaviour here has changed from libwww-perl)
from cookielib import CookieJar, DefaultCookiePolicy
c = CookieJar(DefaultCookiePolicy(rfc2965=True))
interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5",
"foo = bar; version = 1")
cookie = interact_2965(
c, "http://www.acme.com/foo%2f%25/<<%0anewå/æøå",
'bar=baz; path="/foo/"; version=1');
version_re = re.compile(r'^\$version=\"?1\"?', re.I)
self.assertIn("foo=bar", cookie)
self.assertRegexpMatches(cookie, version_re)
cookie = interact_2965(
c, "http://www.acme.com/foo/%25/<<%0anewå/æøå")
self.assertFalse(cookie)
# unicode URL doesn't raise exception
cookie = interact_2965(c, u"http://www.acme.com/\xfc")
python类CookieJar()的实例源码
def test_secure(self):
from cookielib import CookieJar, DefaultCookiePolicy
for ns in True, False:
for whitespace in " ", "":
c = CookieJar()
if ns:
pol = DefaultCookiePolicy(rfc2965=False)
int = interact_netscape
vs = ""
else:
pol = DefaultCookiePolicy(rfc2965=True)
int = interact_2965
vs = "; Version=1"
c.set_policy(pol)
url = "http://www.acme.com/"
int(c, url, "foo1=bar%s%s" % (vs, whitespace))
int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
self.assertTrue(
not c._cookies["www.acme.com"]["/"]["foo1"].secure,
"non-secure cookie registered secure")
self.assertTrue(
c._cookies["www.acme.com"]["/"]["foo2"].secure,
"secure cookie registered non-secure")
def test_domain_mirror(self):
from cookielib import CookieJar, DefaultCookiePolicy
pol = DefaultCookiePolicy(rfc2965=True)
c = CookieJar(pol)
url = "http://foo.bar.com/"
interact_2965(c, url, "spam=eggs; Version=1")
h = interact_2965(c, url)
self.assertNotIn("Domain", h,
"absent domain returned with domain present")
c = CookieJar(pol)
url = "http://foo.bar.com/"
interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
h = interact_2965(c, url)
self.assertIn('$Domain=".bar.com"', h, "domain not returned")
c = CookieJar(pol)
url = "http://foo.bar.com/"
# note missing initial dot in Domain
interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
h = interact_2965(c, url)
self.assertIn('$Domain="bar.com"', h, "domain not returned")
def test_path_mirror(self):
from cookielib import CookieJar, DefaultCookiePolicy
pol = DefaultCookiePolicy(rfc2965=True)
c = CookieJar(pol)
url = "http://foo.bar.com/"
interact_2965(c, url, "spam=eggs; Version=1")
h = interact_2965(c, url)
self.assertNotIn("Path", h, "absent path returned with path present")
c = CookieJar(pol)
url = "http://foo.bar.com/"
interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
h = interact_2965(c, url)
self.assertIn('$Path="/"', h, "path not returned")
def test_url_encoding(self):
# Try some URL encodings of the PATHs.
# (the behaviour here has changed from libwww-perl)
from cookielib import CookieJar, DefaultCookiePolicy
c = CookieJar(DefaultCookiePolicy(rfc2965=True))
interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5",
"foo = bar; version = 1")
cookie = interact_2965(
c, "http://www.acme.com/foo%2f%25/<<%0anew/",
'bar=baz; path="/foo/"; version=1');
version_re = re.compile(r'^\$version=\"?1\"?', re.I)
self.assertTrue("foo=bar" in cookie and version_re.search(cookie))
cookie = interact_2965(
c, "http://www.acme.com/foo/%25/<<%0anew/")
self.assertTrue(not cookie)
# unicode URL doesn't raise exception
cookie = interact_2965(c, u"http://www.acme.com/\xfc")
def getPage(self, url, postdata=None, headers=None):
try:
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
if postdata is None:
if headers is None:
request = urllib2.Request(url)
else:
request = urllib2.Request(url, headers=headers)
else:
if headers is not None:
request = urllib2.Request(url, postdata, headers)
page = opener.open(request)
redirect_url = opener.open(request).geturl() # ?????url
soup = BeautifulSoup(page, 'html.parser')
return page, soup, redirect_url
# ???????
except urllib2.URLError, e:
if hasattr(e, "reason"):
print u"????,????", e.reason
return None
# ?????
def __init__(self, cookiejar=None):
import cookielib
if cookiejar is None:
cookiejar = cookielib.CookieJar()
self.cookiejar = cookiejar
def load(self):
'''Load cookies into a cookiejar'''
cookie_jar = cookielib.CookieJar()
for cookie in self.get_cookies():
cookie_jar.set_cookie(cookie)
return cookie_jar
def load():
"""Try to load cookies from all supported browsers and return combined cookiejar
"""
cookie_jar = cookielib.CookieJar()
for cookie in sorted(_get_cookies(), key=lambda cookie: cookie.expires):
cookie_jar.set_cookie(cookie)
return cookie_jar
def installCookie(self):
if self.__opener is None:
self.__cookies = cookielib.CookieJar()
proxy_support= None
if self.__proxyIp is not None:
proxy_support = urllib.request.ProxyHandler(self.__proxyIp)
self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies),proxy_support)
else:
self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies))
urllib2.install_opener(self.__opener)
def installCookie(self):
if self.__opener is None:
self.__cookies = cookielib.CookieJar()
self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies))
urllib2.install_opener(self.__opener)
def postmsg():
myname = raw_input('Name : ')
mymsg = raw_input('Message :')
submitvalue = "submit"
myidcode = getClientCode()
cjar = cookielib.CookieJar()
myopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cjar))
user_agent = 'Mozilla/4.0 (compatible, MSIE 5.5; Windows NT)'
headers = {'User-Agent': user_agent}
myvalues = [ ('name', myname), ('message', mymsg), ('code', myidcode), ('shout', submitvalue) ]
fencode = urllib.urlencode(myvalues)
rqdata = urllib2.Request('http://'+realhost+'.shoutmix.com/?'+options.smuser+"=process", fencode, headers)
respdata = urllib2.urlopen(rqdata).read()
print "string sent"
def __init__(self, cookiejar=None):
import cookielib
if cookiejar is None:
cookiejar = cookielib.CookieJar()
self.cookiejar = cookiejar
def __init__(self, login, password):
self.__username = login
self.__password = password
# Stworzenie s?oika na ciasteczka ;)
self.__cj = cj = cookielib.CookieJar()
self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
self.__login()
def __init__(self, postdata, url):
threading.Thread.__init__(self)
self.setDaemon(True)
self.url = url
self.postdata = postdata
self.cookie = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
def __init__(self, url):
threading.Thread.__init__(self)
self.setDaemon(True)
self.url = url
self.cookie = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
def build_opener_with_chrome_cookies(domain=None):
#?????
cookie_file_path = os.path.join(os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies')
if not os.path.exists(cookie_file_path):
raise Exception('Cookies file not exist!')
conn = sqlite3.connect(cookie_file_path)
sql = 'select host_key, name, value, path from cookies'
if domain:
sql += ' where host_key like "%{}%"'.format(domain)
cookiejar = cookielib.CookieJar() # No cookies stored yet
for row in conn.execute(sql):
print row
cookie_item = cookielib.Cookie(
version=0, name=row[1], value=row[2],
port=None, port_specified=None,
domain=row[0], domain_specified=None, domain_initial_dot=None,
path=row[3], path_specified=None,
secure=None,
expires=None,
discard=None,
comment=None,
comment_url=None,
rest=None,
rfc2109=False,
)
cookiejar.set_cookie(cookie_item) # Apply each cookie_item to cookiejar
conn.close()
return urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) # Return opener
#build_opener_with_chrome_cookies()
def __init__(self, cookiejar=None):
import cookielib
if cookiejar is None:
cookiejar = cookielib.CookieJar()
self.cookiejar = cookiejar
def cookieAgentFactory(verify_path, connectTimeout=30):
customPolicy = BrowserLikePolicyForHTTPS(
Certificate.loadPEM(FilePath(verify_path).getContent()))
agent = Agent(reactor, customPolicy, connectTimeout=connectTimeout)
cookiejar = cookielib.CookieJar()
return CookieAgent(agent, cookiejar)
def upgradeWiseproxy(proxys):
'''?? ????wiseproxy ????wiseproxy'''
dir_r = os.path.dirname(os.path.abspath(__file__))
etc_c = os.path.join(dir_r, 'wiseproxy.conf')
print etc_c
cf = ConfigParser.ConfigParser()
cf.read(etc_c)
login_url = cf.get('main', 'login_url')
api_url = cf.get('main', 'api_url')
usr_pwd_data = eval(cf.get('main', 'usr_pwd_data'))
headers = {'Content-type': 'application/x-www-form-urlencoded', 'charset': 'utf-8'}
# login
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
req = urllib2.Request(login_url, urlencode(usr_pwd_data), headers)
opener.open(req)
#delete old proxy
queryurl='http://wiseproxy.ops.vobile.org/getProxyList?partitionSearch=buy&proxySearch=&_search=false&nd=1503380267574&rows=500&page=1&sidx=&sord=asc'
#http://wiseproxy.ops.vobile.org/getProxyList?partitionSearch=&proxySearch=120.55.115.209:63267&_search=false&nd=1503563948157&rows=10&page=1&sidx=&sord=asc
oldproxys = opener.open(queryurl).read()
oldproxys=json.loads(oldproxys)
oldproxylist=oldproxys['rows']
for oldproxy in oldproxylist:
proxyid = oldproxy['id']
print proxyid
sys.exit()
if type(proxys)==type([]):
# addproxy
for proxy in proxys:
data = {"partition": "9509", "proxyId": -1, "proxyServerInfo": proxy, "source": "Vultr"}
request = urllib2.Request(api_url, urlencode(data), headers)
response = opener.open(request)
else:
data = {"partition": "9509", "proxyId": -1, "proxyServerInfo": proxys, "source": "Vultr"}
request = urllib2.Request(api_url, urlencode(data), headers)
response = opener.open(request)
opener.close()
#upgradeWiseproxy('192.168.200.252:3127')