這是我寫的python3的版本, 歡迎大家參考和指正
代碼部分:
import?requests
def?url_simple_demo(URL_STR):
????response?=?requests.get(URL_STR)
????print('>>>>>>?headers:')
????print(response.headers)
????print('>>>>>>?body:')
????#?print(response.json())??#?json方法的速度比text快
????print(response.text)
????return
def?url_get_demo(URL_STR):
????url_param?=?{'param1':?'hello',?'param2':?'world'}
????response?=?requests.get(URL_STR,?params=url_param)
????print('>>>>>>?status:?%s?%s'?%?(response.status_code,?response.reason))
????print('>>>>>>?headers:')
????print(response.headers)
????print('>>>>>>?body:')
????#?print(response.json())
????print(response.text)
????return
if?__name__?==?'__main__':
????URL_IP?=?'http://httpbin.org/ip'
????url_simple_demo(URL_IP)
????print('-------分割線-------\n')
????URL_GET?=?'http://httpbin.org/get'
????url_get_demo(URL_GET)輸出結果:
>>>>>>?headers:
{'Connection':?'keep-alive',?'Server':?'meinheld/0.6.1',?'Date':?'Sat,?12?Aug?2017?12:03:43?GMT',?'Content-Type':?'application/json',?'Access-Control-Allow-Origin':?'*',?'Access-Control-Allow-Credentials':?'true',?'X-Powered-By':?'Flask',?'X-Processed-Time':?'0.000636100769043',?'Content-Length':?'31',?'Via':?'1.1?vegur'}
>>>>>>?body:
{
??"origin":?"127.0.0.1"
}
-------分割線-------
>>>>>>?status:?200?OK
>>>>>>?headers:
{'Connection':?'keep-alive',?'Server':?'meinheld/0.6.1',?'Date':?'Sat,?12?Aug?2017?12:03:44?GMT',?'Content-Type':?'application/json',?'Access-Control-Allow-Origin':?'*',?'Access-Control-Allow-Credentials':?'true',?'X-Powered-By':?'Flask',?'X-Processed-Time':?'0.00121283531189',?'Content-Length':?'340',?'Via':?'1.1?vegur'}
>>>>>>?body:
{
??"args":?{
????"param1":?"hello",?
????"param2":?"world"
??},?
??"headers":?{
????"Accept":?"*/*",?
????"Accept-Encoding":?"gzip,?deflate",?
????"Connection":?"close",?
????"Host":?"httpbin.org",?
????"User-Agent":?"python-requests/2.18.2"
??},?
??"origin":?"127.0.0.1",?
??"url":?"http://httpbin.org/get?param1=hello¶m2=world"
}
2017-08-16
666
2018-01-13
哈哈。有幫到我