首页 > 代码库 > Python发一个GET请求

Python发一个GET请求

# -*- coding: utf-8 -*-

try: 
	import httplib2
except ImportError:
	print(‘错误:‘)
	print(‘    httplib2这个库没有找到,程序无法继续运行!‘)
	exit(255)
	
	
def network_get_proc(self, use_cache = True):
		‘‘‘POST动作‘‘‘
		get_request_str = ‘http://192.168.0.54/softlist‘	# 请求URL
		# print(‘->%s‘ % post_request_str)
		if use_cache:
			h = httplib2.Http(‘.cache‘)
		else:
			h = httplib2.Http(‘no‐cache‘)
		if use_cache:
			response, content = h.request(get_request_str)
		else:
			response, content = h.request(get_request_str, headers={‘cache‐control‘:‘no‐cache‘})
		print(‘->status:%d‘ % response.status)
		
		get_ret = False
		if response.status == 200:
			get_ret = True
			print(‘INFO:success!‘)
			print(‘***response***‘)
			print(response)
			print(‘***content***‘)
			print(content.decode(‘utf-8‘))
		else:
			get_ret = False
		return get_ret
		
		

# 应用程序入口
if __name__ == ‘__main__‘:
	post_ret = network_get_proc(False)