本文整理汇总了Python中cookielib.MozillaCookieJar方法的典型用法代码示例。如果您正苦于以下问题:Python cookielib.MozillaCookieJar方法的具体用法?Python cookielib.MozillaCookieJar怎么用?Python cookielib.MozillaCookieJar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cookielib
的用法示例。
在下文中一共展示了cookielib.MozillaCookieJar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_openload
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def read_openload(url):
default_headers = dict()
default_headers[
"User-Agent"] = "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3163.100 Safari/537.36"
default_headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
default_headers["Accept-Language"] = "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3"
default_headers["Accept-Charset"] = "UTF-8"
default_headers["Accept-Encoding"] = "gzip"
cj = cookielib.MozillaCookieJar()
request_headers = default_headers.copy()
url = urllib.quote(url, safe="%/:=&?~#+!$,;'@()*[]")
handlers = [urllib2.HTTPHandler(debuglevel=False)]
handlers.append(NoRedirectHandler())
handlers.append(urllib2.HTTPCookieProcessor(cj))
opener = urllib2.build_opener(*handlers)
req = urllib2.Request(url, None, request_headers)
handle = opener.open(req, timeout=None)
return handle.headers.dict.get('location')
示例2: test_bad_magic
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def test_bad_magic(self):
from cookielib import LWPCookieJar, MozillaCookieJar, LoadError
# IOErrors (eg. file doesn't exist) are allowed to propagate
filename = test_support.TESTFN
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
try:
c.load(filename="for this test to work, a file with this "
"filename should not exist")
except IOError, exc:
# exactly IOError, not LoadError
self.assertEqual(exc.__class__, IOError)
else:
self.fail("expected IOError for invalid filename")
# Invalid contents of cookies file (eg. bad magic string)
# causes a LoadError.
示例3: load_and_merge_cookie_jars
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def load_and_merge_cookie_jars(cookie_jar_paths):
cookie_jar = RequestsCookieJar()
if not cookie_jar_paths:
return cookie_jar
logging.debug("Attempting to load and merge the following cookie files: %s" % cookie_jar_paths)
for f in cookie_jar_paths:
if os.path.isfile(f):
try:
cookies = MozillaCookieJar(f)
cookies.load(ignore_expires=True, ignore_discard=True)
cookie_jar.update(cookies)
except Exception as e:
logging.warning("Unable to load cookie file [%s]: %s" % (f, get_typed_exception(e)))
# Do not preserve expire values from cookies with expires=0 from the file, or requests will not use the cookie
for cookie in iter(cookie_jar):
if not cookie.expires:
cookie.expires = None
return cookie_jar
示例4: login
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def login():
cookie_filename = "cookies.txt"
cookiejar = cookielib.MozillaCookieJar(cookie_filename)
opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),urllib2.HTTPHandler(debuglevel=0),urllib2.HTTPSHandler(debuglevel=0),urllib2.HTTPCookieProcessor(cookiejar))
page = loadPage(opener, "https://www.linkedin.com/")
parse = BeautifulSoup(page, "html.parser")
csrf = parse.find(id="loginCsrfParam-login")['value']
login_data = urllib.urlencode({'session_key': username, 'session_password': password, 'loginCsrfParam': csrf})
page = loadPage(opener,"https://www.linkedin.com/uas/login-submit", login_data)
parse = BeautifulSoup(page, "html.parser")
cookie = ""
try:
cookie = cookiejar._cookies['.www.linkedin.com']['/']['li_at'].value
except:
sys.exit(0)
cookiejar.save()
os.remove(cookie_filename)
return cookie
示例5: get_cookie
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def get_cookie(self):
if os.path.isfile(self.cookie_jar_path):
self.cookie_jar = MozillaCookieJar()
self.cookie_jar.load(self.cookie_jar_path)
# make sure cookie is still valid
if self.check_cookie():
print(" > Re-using previous cookie jar.")
return True
else:
print(" > Could not validate old cookie Jar")
# We don't have a valid cookie, prompt user or creds
print("No existing URS cookie found, please enter Earthdata username & password:")
print("(Credentials will not be stored, saved or logged anywhere)")
# Keep trying 'till user gets the right U:P
while self.check_cookie() is False:
self.get_new_cookie()
return True
# Validate cookie before we begin
示例6: __init__
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def __init__(self):
self.articles = []
self.query = None
self.cjar = MozillaCookieJar()
# If we have a cookie file, load it:
if ScholarConf.COOKIE_JAR_FILE and \
os.path.exists(ScholarConf.COOKIE_JAR_FILE):
try:
self.cjar.load(ScholarConf.COOKIE_JAR_FILE,
ignore_discard=True)
ScholarUtils.log('info', 'loaded cookies file')
except Exception as msg:
ScholarUtils.log('warn', 'could not load cookies file: %s' % msg)
self.cjar = MozillaCookieJar() # Just to be safe
self.opener = build_opener(HTTPCookieProcessor(self.cjar))
self.settings = None # Last settings object, if any
示例7: saveCookies
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def saveCookies(self, cookiejar):
MozillaCookieJar = cookielib.MozillaCookieJar()
for c in cookiejar:
args = dict(vars(c).items())
args['rest'] = args['_rest']
del args['_rest']
c = cookielib.Cookie(**args)
MozillaCookieJar.set_cookie(c)
MozillaCookieJar.save('youdaoCookies', ignore_discard=True)
示例8: loadCookies
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def loadCookies(self):
if os.path.exists('youdaoCookies'):
self.window.debug.appendPlainText('625: Cookie exists!')
MozillaCookieJar = cookielib.MozillaCookieJar()
MozillaCookieJar.load('youdaoCookies', ignore_discard=True)
return MozillaCookieJar
else:
return False
示例9: test_missing_value
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def test_missing_value(self):
from cookielib import MozillaCookieJar, lwp_cookie_str
# missing = sign in Cookie: header is regarded by Mozilla as a missing
# name, and by cookielib as a missing value
filename = test_support.TESTFN
c = MozillaCookieJar(filename)
interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
cookie = c._cookies["www.acme.com"]["/"]["eggs"]
self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, "eggs")
cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
self.assertIsNone(cookie.value)
self.assertEqual(cookie.name, '"spam"')
self.assertEqual(lwp_cookie_str(cookie), (
r'"spam"; path="/foo/"; domain="www.acme.com"; '
'path_spec; discard; version=0'))
old_str = repr(c)
c.save(ignore_expires=True, ignore_discard=True)
try:
c = MozillaCookieJar(filename)
c.revert(ignore_expires=True, ignore_discard=True)
finally:
os.unlink(c.filename)
# cookies unchanged apart from lost info re. whether path was specified
self.assertEqual(
repr(c),
re.sub("path_specified=%s" % True, "path_specified=%s" % False,
old_str)
)
self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
'"spam"; eggs')
示例10: login
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def login(form_data):
url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)'
headers = ('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0')
cookie = cookielib.MozillaCookieJar(cookie_file)
handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
opener.addheaders.append(headers)
req = opener.open(url, form_data)
redirect_result = req.read()
login_pattern = r'location.replace\(\'(.*?)\'\)'
login_url = re.search(login_pattern, redirect_result).group(1)
opener.open(login_url).read()
cookie.save(cookie_file, ignore_discard=True, ignore_expires=True)
示例11: request_image_url
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def request_image_url(image_path):
cookie = cookielib.MozillaCookieJar()
cookie.load(cookie_file, ignore_expires=True, ignore_discard=True)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
image_url = 'http://picupload.service.weibo.com/interface/pic_upload.php?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog'
b = base64.b64encode(file(image_path).read())
data = urllib.urlencode({'b64_data': b})
result = opener.open(image_url, data).read()
result = re.sub(r"<meta.*</script>", "", result, flags=re.S)
image_result = json.loads(result)
image_id = image_result.get('data').get('pics').get('pic_1').get('pid')
return 'http://ww3.sinaimg.cn/large/%s' % image_id
示例12: test_missing_value
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def test_missing_value(self):
from cookielib import MozillaCookieJar, lwp_cookie_str
# missing = sign in Cookie: header is regarded by Mozilla as a missing
# name, and by cookielib as a missing value
filename = test_support.TESTFN
c = MozillaCookieJar(filename)
interact_netscape(c, "http://www.acme.com/", 'eggs')
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
cookie = c._cookies["www.acme.com"]["/"]["eggs"]
self.assertTrue(cookie.value is None)
self.assertEqual(cookie.name, "eggs")
cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
self.assertTrue(cookie.value is None)
self.assertEqual(cookie.name, '"spam"')
self.assertEqual(lwp_cookie_str(cookie), (
r'"spam"; path="/foo/"; domain="www.acme.com"; '
'path_spec; discard; version=0'))
old_str = repr(c)
c.save(ignore_expires=True, ignore_discard=True)
try:
c = MozillaCookieJar(filename)
c.revert(ignore_expires=True, ignore_discard=True)
finally:
os.unlink(c.filename)
# cookies unchanged apart from lost info re. whether path was specified
self.assertEqual(
repr(c),
re.sub("path_specified=%s" % True, "path_specified=%s" % False,
old_str)
)
self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
'"spam"; eggs')
示例13: _GetOpener
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def _GetOpener(self):
"""Returns an OpenerDirector that supports cookies and ignores redirects.
Returns:
A urllib2.OpenerDirector object.
"""
opener = urllib2.OpenerDirector()
opener.add_handler(urllib2.ProxyHandler())
opener.add_handler(urllib2.UnknownHandler())
opener.add_handler(urllib2.HTTPHandler())
opener.add_handler(urllib2.HTTPDefaultErrorHandler())
opener.add_handler(urllib2.HTTPSHandler())
opener.add_handler(urllib2.HTTPErrorProcessor())
if self.save_cookies:
self.cookie_file = os.path.expanduser("~/.codereview_upload_cookies")
self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
if os.path.exists(self.cookie_file):
try:
self.cookie_jar.load()
self.authenticated = True
StatusUpdate("Loaded authentication cookies from %s" %
self.cookie_file)
except (cookielib.LoadError, IOError):
# Failed to load cookies - just ignore them.
pass
else:
# Create an empty cookie file with mode 600
fd = os.open(self.cookie_file, os.O_CREAT, 0600)
os.close(fd)
# Always chmod the cookie file
os.chmod(self.cookie_file, 0600)
else:
# Don't save cookies across runs of update.py.
self.cookie_jar = cookielib.CookieJar()
opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
return opener
示例14: init
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def init(args):
# 设置日志级别
debug_mode = args.debug
log_file = file(os.path.join(os.getcwd(), 'debug.log'), 'w') if debug_mode else sys.stderr
log_level = logging.DEBUG if debug_mode else logging.ERROR
logging.basicConfig(level=log_level, stream=log_file,
format='%(asctime)s %(levelname)s %(name)s - %(message)s')
# 初始化数据
_data['alive'] = True
_data[_RWD] = '/'
# 本地工作目录
lwd = args.local_dir
if lwd and os.path.exists(lwd):
lwd = os.path.abspath(lwd)
else:
lwd = config.get_localhome()
if not lwd.endswith(os.sep):
lwd += os.sep
_data[_LWD] = lwd
# cookie文件
global cookie_file
# cookie_file = args.get('cookie')
# if cookie_file:
# cookie_file = os.path.abspath(cookie_file)
# else:
cookie_file = util.get_data_file('.baidupan.cookie')
# 初始化client
import cookielib
from baidupan import api, tree
global cookie_jar, client
cookie_jar = cookielib.MozillaCookieJar(cookie_file)
if os.path.exists(cookie_file): cookie_jar.load()
client = api.BaiduPanClient(cookie_jar, util.ascii_vcode_handler)
# 初始化远程文件树
global remote_tree
remote_tree = tree.RemoteTree(client)
示例15: _urllib2Opener
# 需要导入模块: import cookielib [as 别名]
# 或者: from cookielib import MozillaCookieJar [as 别名]
def _urllib2Opener():
"""
This function creates the urllib2 OpenerDirector.
"""
debugMsg = "creating HTTP requests opener object"
logger.debug(debugMsg)
handlers = [proxyHandler, authHandler, redirectHandler, rangeHandler, httpsHandler]
if not conf.dropSetCookie:
if not conf.loadCookies:
conf.cj = cookielib.CookieJar()
else:
conf.cj = cookielib.MozillaCookieJar()
resetCookieJar(conf.cj)
handlers.append(urllib2.HTTPCookieProcessor(conf.cj))
# Reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
if conf.keepAlive:
warnMsg = "persistent HTTP(s) connections, Keep-Alive, has "
warnMsg += "been disabled because of its incompatibility "
if conf.proxy:
warnMsg += "with HTTP(s) proxy"
logger.warn(warnMsg)
elif conf.authType:
warnMsg += "with authentication methods"
logger.warn(warnMsg)
else:
handlers.append(keepAliveHandler)
opener = urllib2.build_opener(*handlers)
urllib2.install_opener(opener)