def login(i,j,ur):
ua = UserAgent()
cookiejar =cookielib.LWPCookieJar()
br = mechanize.Browser()
br.set_cookiejar(cookiejar)
#br.set_debug_http(True)
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.addheaders = [('User-Agent', ua.random), ('Accept', '*/*')]
url = "http://www."+ur+"/"
br.open(url)
br.select_form(nr = 1)
br.form['username'] = i
br.form['password'] = j
br.submit()
if len(br.geturl()) == 72:
return True
else:
return False
python类LWPCookieJar()的实例源码
def __init__(self):
lf.input_account()
self._url = gl.g_zhihu_host
self.account = gl.g_zhihu_account
self._verify_code = ""
self.default_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0",
"connection":"keep-alive"}
self._is_init = False
self.login_success = False
self.logger = ZhihuLog.creatlogger(self.__class__.__name__)
self.session = requests.Session()
self.session.headers = self.default_headers
self.session.cookies = cookielib.LWPCookieJar(gl.g_config_folder + 'cookiejar')
#if os.path.exists(gl.g_config_folder+'cookiejar'):
# self.session.cookies.load(ignore_discard=True)
#self.login_success = True
if not self.login_success :
self.login()
pass
def get_cookies():
"""???? cookies, ?????."""
headers = {
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
"(KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36"
)
}
save_cookies_file = 'cookies.txt'
jar = cookielib.LWPCookieJar(save_cookies_file)
sess = requests.session()
sess.headers = headers
sess.cookies = jar
sess.get('http://tieba.baidu.com/')
jar.save(ignore_expires=True, ignore_discard=True)
return jar
def login(i,j,ur):
ua = UserAgent()
cookiejar =cookielib.LWPCookieJar()
br = mechanize.Browser()
br.set_cookiejar(cookiejar)
#br.set_debug_http(True)
br.set_handle_robots(False)
br.set_handle_refresh(False)
br.addheaders = [('User-Agent', ua.random), ('Accept', '*/*')]
url = "http://www."+ur+"/"
br.open(url)
br.select_form(nr = 1)
br.form['username'] = i
br.form['password'] = j
br.submit()
if len(br.geturl()) == 72:
return True
else:
return False
def __init__(self):
self.cookieJar = cookielib.LWPCookieJar()
self.session = None
self.clientHeader = None
self.average_download_speed = 0.0
self.manifest_playlist = None
self.media_list = None
self.selected_bandwidth_index = None
self.use_proxy = False
self.proxy = None
self.g_stopEvent = None
self.maxbitrate = 0
self.url = None
self.media_buffer = {}
self.requested_segment = None
self.key = None
self.queue = None
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.
try:
f = open(filename, "w")
f.write("oops\n")
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
self.assertRaises(LoadError, c.load, filename)
finally:
try: os.unlink(filename)
except OSError: pass
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.
try:
f = open(filename, "w")
f.write("oops\n")
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
self.assertRaises(LoadError, c.load, filename)
finally:
try: os.unlink(filename)
except OSError: pass
def set_cookie(cookie_file):
"""
@brief Load cookie from file
@param cookie_file
@param user_agent
@return cookie, LWPCookieJar
"""
cookie = cookielib.LWPCookieJar(cookie_file)
try:
cookie.load(ignore_discard=True)
except:
Log.error(traceback.format_exc())
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
opener.addheaders = Constant.HTTP_HEADER_USERAGENT
urllib2.install_opener(opener)
return cookie
def __init__(self, headers = {}, debug = True, p = ''):
self.timeout = 10
self.br = mechanize.Browser() #???br
self.cj = cookielib.LWPCookieJar()
self.br.set_cookiejar(self.cj)#??cookie
self.br.set_handle_equiv(True)#????http equiv
self.br.set_handle_gzip(True)#??????
self.br.set_handle_redirect(True)#???????
self.br.set_handle_referer(True)#??????referer
self.br.set_handle_robots(False)#????robots??
self.br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
self.br.addheaders = [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')]
self.debug = debug
#debug?????????????
if self.debug:
self.br.set_debug_http(True)
self.br.set_debug_redirects(True)
self.br.set_debug_responses(True)
#headers
for keys in headers.keys():
self.br.addheaders += [(key, headers[key]), ]
#proxy
if len(p) > 0 and p != 'None' and p != None and p != 'NULL':
self.br.set_proxies({'http': p})
def parse(self, response):
topic_xpath_rule = '//li[@class="zm-topic-cat-item"]/a/text()'
topic_names = response.selector.xpath(topic_xpath_rule).extract()
topic_xpath_rule = '//li[@class="zm-topic-cat-item"]/@data-id'
topic_ids = response.selector.xpath(topic_xpath_rule).extract()
# for i in range(len(topic_ids)):
print("?30???")
# for i in range(10):
for i in range(len(topic_ids)):
params = {"topic_id": int(topic_ids[i]), "offset": 0, "hash_id": "d17ff3d503b2ebce086d2f3e98944d54"}
yield FormRequest(
url='https://www.zhihu.com/node/TopicsPlazzaListV2',
method='POST',
# headers=self.set_headers2('https://www.zhihu.com/topics'),
headers=self.set_headers('https://www.zhihu.com/topics'),
cookies=cookielib.LWPCookieJar(filename='cookies'),
# formdata={'method': 'next', 'params': '{"topic_id":988,"offset":0,"hash_id":"d17ff3d503b2ebce086d2f3e98944d54"}'},
formdata={'method': 'next', 'params': str(params).replace("\'", "\"").replace(" ", "")},
callback=self.topic_parse,
meta={'topic_name': topic_names[i]}
)
def requestJSON(self, url, headers, body=None, method=None):
addon_profile_path = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile'))
cj = cookielib.LWPCookieJar(os.path.join(addon_profile_path, 'cookies.lwp'))
try: cj.load(os.path.join(addon_profile_path, 'cookies.lwp'),ignore_discard=True)
except: pass
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = headers
try:
request = urllib2.Request(url, body)
if method == 'DELETE': request.get_method = lambda: method
response = opener.open(request)
json_source = json.load(response)
response.close()
self.saveCookie(cj)
except HTTPError as e:
if e.code == 403:
msg = 'Your device is not authorized to view the selected stream.\n Would you like to authorize this device now?'
dialog = xbmcgui.Dialog()
answer = dialog.yesno('Account Not Authorized', msg)
if answer: self.registerDevice()
sys.exit(0)
return json_source
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.
try:
f = open(filename, "w")
f.write("oops\n")
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
self.assertRaises(LoadError, c.load, filename)
finally:
try: os.unlink(filename)
except OSError: pass
def getURL(url, host=BASE_URL.split('//')[1], useCookie=False, silent=False, headers=None):
cj = cookielib.LWPCookieJar()
if useCookie:
if isinstance(useCookie, bool):
cj = mechanizeLogin()
else:
cj = useCookie
if isinstance(cj, bool):
return False
dispurl = re.sub('(?i)%s|%s|&token=\w+' % (tvdb, tmdb), '', url).strip()
if not silent:
Log('getURL: ' + dispurl)
if not headers:
headers = [('User-Agent', UserAgent), ('Host', host)]
try:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPRedirectHandler)
opener.addheaders = headers
usock = opener.open(url)
response = usock.read()
usock.close()
except urllib2.URLError, e:
Log('Error reason: %s' % e, xbmc.LOGERROR)
return False
return response
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.
try:
f = open(filename, "w")
f.write("oops\n")
for cookiejar_class in LWPCookieJar, MozillaCookieJar:
c = cookiejar_class()
self.assertRaises(LoadError, c.load, filename)
finally:
try: os.unlink(filename)
except OSError: pass
def __init__(self):
self.url = None
self.account = None
self.default_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0",
"connection":"keep-alive",
"Accept-Encoding": "gzip"}
self._is_init = False
self.load_cookies_success = False
self.login_success = False
self.logger = ZhihuLog.creatlogger(self.__class__.__name__)
self.session = requests.Session()
self.session.headers = self.default_headers
self.session.cookies = cookielib.LWPCookieJar(gl.g_config_folder + 'cookiejar')
if os.path.exists(gl.g_config_folder+'cookiejar'):
self.session.cookies.load(ignore_discard=True)
self.load_cookies_success = True
def loginStatus_s(self, response):
soup = BeautifulSoup(response.text,"html.parser")
if soup.find("div",{"class":"me"}) == None:
print u"-----------????---------".decode(code).encode('gbk')
#print 'content ', response.content
login_uid = re.findall(r'href=\"/([0-9]+)/info', response.content,re.I)
login_uid = login_uid[0]
cookieInfo = requests.utils.dict_from_cookiejar(session.cookies)
cj = cookielib.LWPCookieJar('loginInfo_'+login_uid+'.txt')
requests.utils.cookiejar_from_dict(cookieInfo, cj)
cj.save('cookies/loginInfo_'+login_uid+'.txt', ignore_discard=True, ignore_expires=True)
with open('login_users.txt', 'a') as f: #?????uid, ??????
f.write(login_uid+'\n')
f.close()
else:
print u"-----------???????????????????-----------"
self.loginSimulation()
def loginStatus_s(self, response):
soup = BeautifulSoup(response.text,"html.parser")
if soup.find("div",{"class":"me"}) == None:
print u"-----------????---------".decode(code).encode('gbk')
print 'content ', response.content
login_uid = re.findall(r'href=\"/([0-9]+)/info', response.content,re.I)
login_uid = login_uid[0]
print 'uid:',login_uid
cookieInfo = requests.utils.dict_from_cookiejar(session.cookies)
cj = cookielib.LWPCookieJar('loginInfo_'+login_uid+'.txt')
requests.utils.cookiejar_from_dict(cookieInfo, cj)
cj.save('cookies/loginInfo_'+login_uid+'.txt', ignore_discard=True, ignore_expires=True)
with open('login_users.txt', 'a') as f: #?????uid, ??????
f.write(login_uid+'\n')
f.close()
else:
print u"-----------???????????????????-----------"
#self.loginSimulation()
def login(username, password):
br = mechanize.Browser()
login_url = "http://cyclebabac.com/wp-login.php"
# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Want debugging messages? Uncomment this
#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
# Perform the actual login
br.open(login_url)
br.select_form(nr=0)
br.form['log'] = str(username)
br.form['pwd'] = str(password)
br.submit()
return br
def proxy_identify(proxy, url):
cookie = cookielib.LWPCookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
proxy_support = urllib2.ProxyHandler({'http': proxy})
opener = urllib2.build_opener(proxy_support, handler)
try:
response = opener.open(url, timeout=3)
if response.code == 200:
c = ''
for item in cookie:
c += item.name+'='+item.value+';'
print c
IpProxy.sogou_cookie.append(c)
return True
except Exception, error:
print error
return False
def check_login(self):
expired_cookies = True
addon_profile_path = xbmc.translatePath(self.addon.getAddonInfo('profile'))
try:
cj = cookielib.LWPCookieJar()
cj.load(os.path.join(addon_profile_path, 'cookies.lwp'), ignore_discard=True)
if self.npsso != '':
for cookie in cj:
if cookie.name == 'npsso':
expired_cookies = cookie.is_expired()
break
except:
pass
if expired_cookies:
self.login()
def __init__(self):
# ????
self.proxy_url = proxyList[3]
self.proxy = urllib2.ProxyHandler({"http": self.proxy_url})
# ??
self.hostURL = 'http://book.douban.com/tag/'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.47 (KHTML, like Gecko)'
' Chrome/48.1.2524.116 Safari/537.36',
'Referer': 'http://book.douban.com/',
'Host': 'book.douban.com',
'Upgrade-Insecure-Requests': '1',
'Connection': 'keep-alive'
}
# opener??
self.cookie = cookielib.LWPCookieJar()
self.cookieHandler = urllib2.HTTPCookieProcessor(self.cookie)
self.opener = urllib2.build_opener(self.cookieHandler, self.proxy, urllib2.HTTPHandler)
# ????????????
def __init__(self, cookie_location="cookie.txt"):
"""
Base constructor. Responsible for setting up the requests object and cookie jar.
All configuration values should be set prior to constructing an object of this
type; changes to configuration will not take effect on variables which already
exist.
"""
self.session = requests.Session()
self.session.cookies = cookielib.LWPCookieJar(cookie_location)
try:
self.session.cookies.load()
except IOError:
# Cookie file doesn't exist.
pass
self.session.headers.update(self.default_headers)
self.session.params.update(self.default_params)
def __init__(self, settings_folder, country, debug=False):
self.debug = debug
self.country = country
self.settings_folder = settings_folder
self.cookie_jar = cookielib.LWPCookieJar(os.path.join(self.settings_folder, 'cookie_file'))
self.tempdir = os.path.join(settings_folder, 'tmp')
if not os.path.exists(self.tempdir):
os.makedirs(self.tempdir)
self.deviceid_file = os.path.join(settings_folder, 'deviceId')
self.http_session = requests.Session()
self.device_key = 'xdk-%s' % self.country
self.base_url = 'https://content.viaplay.{0}/{1}'.format(self.country, self.device_key)
self.login_api = 'https://login.viaplay.%s/api' % self.country
try:
self.cookie_jar.load(ignore_discard=True, ignore_expires=True)
except IOError:
pass
self.http_session.cookies = self.cookie_jar
def create_browser():
br = mechanize.Browser() # Create basic browser
cj = cookielib.LWPCookieJar() # Create cookiejar to handle cookies
br.set_cookiejar(cj) # Set cookie jar for our browser
br.set_handle_equiv(True) # Allow opening of certain files
br.set_handle_gzip(True) # Allow handling of zip files
br.set_handle_redirect(True) # Automatically handle auto-redirects
br.set_handle_referer(True)
br.set_handle_robots(False) # ignore anti-robots.txt
# Necessary headers to simulate an actual browser
br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'),
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'),
('Accept-Encoding', 'gzip,deflate,sdch'),
('Accept-Language', 'en-US,en;q=0.8,fr;q=0.6'),
('Connection', 'keep-alive')
]
return br
def clearCookie(self, cookiefile, leaveNames=[], removeNames=None, ignore_discard = True):
try:
toRemove = []
if not self.useMozillaCookieJar:
cj = cookielib.LWPCookieJar()
else:
cj = cookielib.MozillaCookieJar()
cj.load(cookiefile, ignore_discard = ignore_discard)
for cookie in cj:
if cookie.name not in leaveNames and (None == removeNames or cookie.name in removeNames):
toRemove.append(cookie)
for cookie in toRemove:
cj.clear(cookie.domain, cookie.path, cookie.name)
cj.save(cookiefile, ignore_discard = ignore_discard)
except Exception:
printExc()
return False
return True
def getInfo(ipaddr, userAgent, proxz, hostname):
WEBFORM_NAME = 'search'
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.set_handle_equiv(True)
browser.set_handle_referer(True)
browser.set_handle_redirect(True)
browser.addheaders = userAgent
# browser.set_proxies(proxz)
cookie_jar = cookielib.LWPCookieJar()
browser.set_cookiejar(cookie_jar)
page = browser.open('https://apps.db.ripe.net/search/query.html')
for form in browser.forms():
if form.name == WEBFORM_NAME:
browser.select_form(WEBFORM_NAME)
browser.form['search:queryString'] = ipaddr
browser.form['search:sources'] = ['GRS']
submission = browser.submit().read()
parsed_submission = BeautifulSoup(submission, 'html.parser')
print ipaddr, '/',hostname
for mainIndex in parsed_submission.find_all('ul', {'class': 'attrblock'}):
for i, li in enumerate(mainIndex.findAll('li')):
if i in range(0, 2):
print '[+] ', li.text
print '\n ########## \n'
def __init__(self):
self.br = mechanize.Browser()
#self.cj = cookielib.LWPCookieJar()
self.cj = cookielib.MozillaCookieJar()
self.br.set_cookiejar(self.cj)
self.br.set_handle_equiv(True)
self.br.set_handle_referer(True)
self.br.set_handle_robots(False)
self.br.addheaders = [('User-agent', 'Firefox')]
self.item_url = 'http://shop.bdgastore.com/collections/footwear/products/y-3-pureboost-zg'
# Create variables for user credentials and a function to import them
def send_cookie_auth(self, connection):
"""Include Cookie Authentication data in a header"""
cj = cookielib.LWPCookieJar()
cj.load(self.cookiefile)
for cookie in cj:
if cookie.name == 'UUID':
uuidstr = cookie.value
connection.putheader(cookie.name, cookie.value)
## override the send_host hook to also send authentication info
def get_file(self, url, quality):
self.cookieJar = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cookieJar),
urllib2.HTTPRedirectHandler(),
urllib2.HTTPHandler(debuglevel=0))
self.opener.addheaders = [('User-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36")]
forms = {"youtubeURL": url,
'quality':quality
}
data = urllib.urlencode(forms)
req = urllib2.Request('http://www.convertmemp3.com/',data)
res = self.opener.open(req)
self.convhtml = res.read()
def __urllib2Opener():
"""
This function creates the urllib2 OpenerDirector.
"""
global authHandler
global proxyHandler
debugMsg = "creating HTTP requests opener object"
logger.debug(debugMsg)
conf.cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(proxyHandler, authHandler, urllib2.HTTPCookieProcessor(conf.cj))
urllib2.install_opener(opener)