本文整理匯總了Python中Httpy.Httpy.clear_cookies方法的典型用法代碼示例。如果您正苦於以下問題:Python Httpy.clear_cookies方法的具體用法?Python Httpy.clear_cookies怎麽用?Python Httpy.clear_cookies使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Httpy.Httpy
的用法示例。
在下文中一共展示了Httpy.clear_cookies方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from Httpy import Httpy [as 別名]
# 或者: from Httpy.Httpy import clear_cookies [as 別名]
class ReddiWrap:
"""
Class for interacting with reddit.com
Uses reddit's API.
"""
def __init__(self, user='', password='', user_agent=None):
"""
Initializes instance fields, sets user agent.
Logs into reddit if user and password are given.
"""
# Default user agent is awesome!
if user_agent == None:
user_agent = 'ReddiWrap'
# Create object we will use to communicate with reddit's servers
self.web = Httpy(user_agent=user_agent)
self.modhash = '' # Hash used to authenticate/interact with user account
self.last_url = '' # The last URL retrieved
self.before = None # ID pointing to 'previous' page
self.after = None # ID pointing to 'next' page
self.logged_in = False # Flag to detect if we are logged in or not
# Sets instance fields, logs in user if needed.
self.login(user, password)
####################
# LOGGING IN & OUT #
####################
def login(self, user='', password=''):
"""
Clears cookies/modhash, then logs into reddit if applicable.
Logs out user if user or password is '' or None
Returns 0 if login (or logout) is successful,
Returns 1 if user/pass is invalid,
Returns 2 if login rate limit is reached,
Returns -1 if some unknown error is encountered
"""
self.web.clear_cookies() # Removes any traces of previous activity
self.modhash = ''
self.logged_in = False
if user == '' or user == None or \
password == '' or password == None:
# "Log out"
self.user = ''
self.password = ''
return 0
self.user = user
self.password = password
dict = {}
dict['user'] = self.user
dict['passwd'] = self.password
dict['api_type'] = 'json'
r = self.web.post('http://www.reddit.com/api/login/%s' % self.user, dict)
if "WRONG_PASSWORD" in r:
# Invalid password
return 1
elif 'RATELIMIT' in r:
# Rate limit reached.
return 2
else: #if 'redirect' in r:
js = json.loads(r)
if js.get('json') == None or js['json'].get('data') == None:
return -1
# Correct password.
self.logged_in = True
self.modhash = js['json']['data']['modhash']
return 0
# Unexpected response.
return -1
def logout(self):
"""
"Logs out": Clears cookies, resets modhash.
"""
self.switch_user('', '')
################
# WEB REQUESTS #
################
@staticmethod
def fix_url(url):
"""
'Corrects' a given URL as needed. Ensures URL will function with API properly.
Ensures:
#.........這裏部分代碼省略.........