本文整理汇总了Python中Httpy.Httpy.post方法的典型用法代码示例。如果您正苦于以下问题:Python Httpy.post方法的具体用法?Python Httpy.post怎么用?Python Httpy.post使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Httpy.Httpy
的用法示例。
在下文中一共展示了Httpy.post方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from Httpy import Httpy [as 别名]
# 或者: from Httpy.Httpy import post [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:
#.........这里部分代码省略.........