这是我写的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" }