當前位置: 首頁>>代碼示例>>Python>>正文


Python Httpy.post方法代碼示例

本文整理匯總了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:
#.........這裏部分代碼省略.........
開發者ID:4pr0n,項目名稱:irarchives,代碼行數:103,代碼來源:ReddiWrap.py


注:本文中的Httpy.Httpy.post方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。