Demo entry 6645031

py

   

Submitted by anonymous on Oct 08, 2017 at 10:24
Language: Python. Code size: 1.3 kB.

	def getResponse(self,url ,use_sesession = False):
		connect_times = 0
		# 最大重连数
		requester = None
		response = None
		headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"}
		# 是否持久化
		if use_sesession:
			requester = self.session
			self.setCookie(self.getCookieJarFromCookieDict(self.getCookieDictFromStr(self.getRandomCookieStr())))
		else:
			requester = requests

		while connect_times <3:
			try:
				response = requester.get(url,headers = headers)
				print response.url,response.ok,connect_times + 1
				if response.ok and response.headers["Content-Type"] == "application/json; charset=utf-8":
					connect_times = 4
				# 当返回的是网页且需要刷新时
				elif response.headers["Content-Type"] == "text/html":
					for line in response.content.split("\n"):
						if "location.replace" in line:
							url = line.strip().replace('location.replace("','').replace('");','')
							break
			except Exception as e:
				print e
				print "connect fail times: %s" %(connect_times + 1,)

				# 多次重连 等待后再次重连
				if connect_times >=2:
					time.sleep(random.choice(range(2*connect_times,4*connect_times)))
			finally:
				connect_times += 1
		return response

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).