本文整理汇总了Python中http.cookiejar.CookieJar.clear方法的典型用法代码示例。如果您正苦于以下问题:Python CookieJar.clear方法的具体用法?Python CookieJar.clear怎么用?Python CookieJar.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.cookiejar.CookieJar
的用法示例。
在下文中一共展示了CookieJar.clear方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clear
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import clear [as 别名]
def clear(self, domain=None, path=None, name=None):
CookieJar.clear(self, domain, path, name)
where_parts = []
sql_params = []
if domain is not None:
where_parts.append("host = ?")
sql_params.append(domain)
if path is not None:
where_parts.append("path = ?")
sql_params.append(path)
if name is not None:
where_parts.append("name = ?")
sql_params.append(name)
where = " AND ".join(where_parts)
if where:
where = " WHERE " + where
def clear(cur):
cur.execute("DELETE FROM moz_cookies%s" % where,
tuple(sql_params))
self._transaction(clear)
示例2: Browser
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import clear [as 别名]
class Browser(object):
def __init__(self):
self.cookiejar = CookieJar()
self._cookie_processor = HTTPCookieProcessor(self.cookiejar)
self.form = None
self.url = "http://0.0.0.0:8080/"
self.path = "/"
self.status = None
self.data = None
self._response = None
self._forms = None
@property
def text(self):
return self.data.decode('utf-8')
def reset(self):
"""Clears all cookies and history."""
self.cookiejar.clear()
def build_opener(self):
"""Builds the opener using (urllib2/urllib.request).build_opener.
Subclasses can override this function to prodive custom openers.
"""
return urllib_build_opener()
def do_request(self, req):
if DEBUG:
print('requesting', req.get_method(), req.get_full_url())
opener = self.build_opener()
opener.add_handler(self._cookie_processor)
try:
self._response = opener.open(req)
except HTTPError as e:
self._response = e
self.url = self._response.geturl()
self.path = get_selector(Request(self.url))
self.data = self._response.read()
self.status = self._response.code
self._forms = None
self.form = None
return self.get_response()
def open(self, url, data=None, headers={}):
"""Opens the specified url."""
url = urljoin(self.url, url)
req = Request(url, data, headers)
return self.do_request(req)
def show(self):
"""Opens the current page in real web browser."""
f = open('page.html', 'w')
f.write(self.data)
f.close()
url = 'file://' + os.path.abspath('page.html')
webbrowser.open(url)
def get_response(self):
"""Returns a copy of the current response."""
return addinfourl(BytesIO(self.data), self._response.info(), self._response.geturl())
def get_soup(self):
"""Returns beautiful soup of the current document."""
import BeautifulSoup
return BeautifulSoup.BeautifulSoup(self.data)
def get_text(self, e=None):
"""Returns content of e or the current document as plain text."""
e = e or self.get_soup()
return ''.join([htmlunquote(c) for c in e.recursiveChildGenerator()
if isinstance(c, text_type)])
def _get_links(self):
soup = self.get_soup()
return [a for a in soup.findAll(name='a')]
def get_links(self, text=None, text_regex=None, url=None, url_regex=None, predicate=None):
"""Returns all links in the document."""
return self._filter_links(self._get_links(),
text=text, text_regex=text_regex, url=url, url_regex=url_regex, predicate=predicate)
def follow_link(self, link=None, text=None, text_regex=None, url=None, url_regex=None, predicate=None):
if link is None:
links = self._filter_links(self.get_links(),
text=text, text_regex=text_regex, url=url, url_regex=url_regex, predicate=predicate)
link = links and links[0]
if link:
return self.open(link['href'])
else:
raise BrowserError("No link found")
def find_link(self, text=None, text_regex=None, url=None, url_regex=None, predicate=None):
#.........这里部分代码省略.........
示例3: MyShowsRu
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import clear [as 别名]
class MyShowsRu(object):
""" work with api.myshows.ru """
def __init__(self, config_name_name):
cfg_file = file(config_name_name)
self.config = config.Config(cfg_file)
logging.info('Config file %s loaded!', config_name_name)
self.cookie_jar = CookieJar()
self.opener = build_opener(
HTTPCookieProcessor(self.cookie_jar)
)
self.logged_ = False
self.list_loaded_ = False
self.api_url = 'http://' + self.config.api_domain
self.shows_data = {}
self.episodes_data = {}
self.watched_data = {}
def do_login(self):
""" authorization """
if self.logged_:
return
try:
req_data = urllib.urlencode({
'login': self.config.login.name,
'password': self.config.login.md5pass
})
logging.debug(
'Login, url: %s%s, data: %s', self.api_url, self.config.url.login, req_data
)
request = Request(
self.api_url + self.config.url.login, req_data
)
handle = self.opener.open(request)
logging.debug('Login result: %s/%s', handle.headers, handle.read())
self.cookie_jar.clear(
self.config.api_domain, '/', 'SiteUser[login]'
)
self.cookie_jar.clear(
self.config.api_domain, '/', 'SiteUser[password]'
)
except HTTPError as ex:
if ex.code == 403:
stderr.write('Bad login name or password!\n')
else:
stderr.write('Login error!\n')
logging.debug('HTTP error #%s: %s\n', ex.code, ex.read())
exit(1)
except URLError as ex:
stderr.write('Login error!\n')
logging.debug('URLError - %s\n', ex.reason)
exit(1)
self.logged_ = True
def load_shows(self):
""" load user shows """
if self.list_loaded_:
return
if not self.logged_:
self.do_login()
logging.debug('Login: %s%s', self.api_url, self.config.url.list_shows)
request = Request(
self.api_url + self.config.url.list_shows
)
handle = self.opener.open(request)
self.shows_data = json.loads(handle.read())
self.list_loaded_ = True
def list_all_shows(self):
""" list all user shows """
self.load_shows()
print()
for show_id in sorted(
self.shows_data, key=lambda show_id: self.shows_data[show_id]['title']
):
next_show = self.shows_data[show_id]
if next_show['watchedEpisodes'] <= 0:
show_sign = '-'
elif next_show['watchedEpisodes'] < next_show['totalEpisodes']:
show_sign = '+'
else:
show_sign = ' '
alias = self.alias_by_title(next_show['title'])
if not alias:
alias = '-'
print('{0}{1}({7}): {2}/{3} ({4}%), rating = {5}({6})'.format(
show_sign,
tr_out(next_show['title']),
# next_show['ruTitle'],
next_show['watchedEpisodes'], next_show['totalEpisodes'],
100 * next_show['watchedEpisodes'] / next_show['totalEpisodes'],
next_show['rating'],
next_show['watchStatus'][0],
alias
))
print()
#.........这里部分代码省略.........
示例4: Site
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import clear [as 别名]
class Site():
def __init__(self,
host=None,
apiurl='/w/api.php',
timeout=100,
srlimit=500,
apfrom=None,
aplimit=5000,
bllimit=5000,
aulimit=5000,
aclimit=5000,
rclimit=5000,
lelimit=5000,
):
if not host:
raise(Exception("host not defined"))
self.host = host
self.apiurl = apiurl
self.url = '%s%s' % (self.host, self.apiurl)
self.format = 'json'
self.cj = CookieJar()
self.opener = urllib.request.build_opener(
urllib.request.HTTPCookieProcessor(self.cj)
)
self.token = None
self.defaults = {}
self.defaults['srlimit'] = srlimit
self.defaults['aplimit'] = aplimit
self.defaults['aclimit'] = aclimit
self.defaults['bllimit'] = bllimit
self.defaults['rclimit'] = rclimit
self.defaults['lelimit'] = lelimit
self.srlimit = srlimit
self.apfrom = apfrom
self.aplimit = aplimit
self.bllimit = bllimit
self.aulimit = aulimit
self.aclimit = aclimit
self.rclimit = rclimit
self.lelimit = lelimit
self.search_info = {}
self.aufinished = False
def return_json(self, data):
return json.loads(bytes.decode(data, 'utf-8'))
def sitematrix(self):
t = {}
t['action'] = 'sitematrix'
t['format'] = self.format
params = urllib.parse.urlencode(t)
f = self.opener.open('%s?%s' % (self.url, params))
return self.return_json(f.read())
def login(self, username=None, password=None):
self.username = username
t = {}
t['action'] = 'login'
t['lgname'] = username
t['lgpassword'] = password
t['format'] = self.format
self.cj.clear()
params = urllib.parse.urlencode(t)
if username:
f = self.opener.open(self.url, params.encode('utf-8'))
d = f.read()
try:
d = self.return_json(d)
self.token = d['login']['token']
except Exception as e:
raise(Exception('Unable to login:', e))
if d['login']['result'] == 'NeedToken':
t['lgtoken'] = d['login']['token']
params = urllib.parse.urlencode(t)
f = self.opener.open(self.url, params.encode('utf-8'))
d = f.read()
try:
d = self.return_json(d)
self.token = d['login']['lgtoken']
except Exception as e:
raise(Exception('Unable to login:', e))
def logout(self):
t = {}
t['action'] = 'logout'
t['format'] = self.format
params = urllib.parse.urlencode(t)
f = self.opener.open('%s?%s' % (self.url, params))
d = f.read()
try:
d = self.return_json(d)
except Exception as e:
raise(Exception('Already logged out'))
def list_backlinks(self, title=None, blcontinue=False, blfilterredir='all', blredirect=False):
t = {}
t['format'] = self.format
t['action'] = 'query'
t['list'] = 'backlinks'
t['bllimit'] = self.bllimit
#.........这里部分代码省略.........