├── .gitignore ├── ch2 ├── notes.md ├── requests_demo.py └── urllib_demo.py ├── ch3 └── main.py ├── ch4 ├── api_response.py ├── hook_response.py └── img_response.py ├── ch5 └── authen.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /ch2/notes.md: -------------------------------------------------------------------------------- 1 | * [什么是HTTP协议?](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) 2 | * [RFC7230](https://tools.ietf.org/html/rfc7230#page-19) 3 | * [urllib和urllib2区别](http://ver007.com/2015/09/22/276.html) 4 | -------------------------------------------------------------------------------- /ch2/requests_demo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | 4 | URL_IP = 'http://localhost:8000/ip' 5 | URL_GET = 'http://localhost:8000/get' 6 | 7 | 8 | def use_simple_requests(): 9 | response = requests.get(URL_IP) 10 | print '>>>>Response Headers:' 11 | print response.headers 12 | print '>>>>Response body:' 13 | print response.text 14 | 15 | 16 | def use_params_requests(): 17 | params = {'param1': 'hello', 'param2': 'world'} 18 | response = requests.get(URL_GET, params=params) 19 | print '>>>>Response Headers:' 20 | print response.headers 21 | print '>>>>Status Code:' 22 | print response.status_code 23 | print '>>>>Reason:' 24 | print response.reason 25 | print '>>>>Request body:' 26 | print response.text 27 | 28 | if __name__ == '__main__': 29 | print '>>>Use simple requests:' 30 | use_simple_requests() 31 | print '' 32 | print '>>>Use params requests:' 33 | use_params_requests() 34 | -------------------------------------------------------------------------------- /ch2/urllib_demo.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import urllib 3 | import urllib2 4 | 5 | URL_IP = 'http://localhost:8000/ip' 6 | URL_GET = 'http://localhost:8000/get' 7 | 8 | 9 | def use_simple_urllib2(): 10 | response = urllib2.urlopen(URL_IP) 11 | print '>>>>Response Headers:' 12 | print response.info() 13 | print '>>>>Response body:' 14 | print ''.join([line for line in response.readlines()]) 15 | 16 | 17 | def use_params_urllib2(): 18 | params = urllib.urlencode({'param1': 'hello', 'param2': 'world'}) 19 | response = urllib2.urlopen('?'.join([URL_GET, '%s']) % params) 20 | print '>>>>Response Headers:' 21 | print response.info() 22 | print '>>>>Status Code:' 23 | print response.getcode() 24 | print '>>>>Request body:' 25 | print ''.join([line for line in response.readlines()]) 26 | 27 | if __name__ == '__main__': 28 | print '>>>Use simple urllib2:' 29 | use_simple_urllib2() 30 | print '' 31 | print '>>>Use params urllib2:' 32 | use_params_urllib2() 33 | -------------------------------------------------------------------------------- /ch3/main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import json 3 | import requests 4 | from requests import exceptions 5 | 6 | URL = 'https://api.github.com' 7 | 8 | 9 | def build_uri(endpoint): 10 | return '/'.join([URL, endpoint]) 11 | 12 | 13 | def better_print(json_str): 14 | return json.dumps(json.loads(json_str), indent=4) 15 | 16 | 17 | def request_method(): 18 | response = requests.get(build_uri('user/emails'), auth=('imoocdemo', 'imoocdemo123')) 19 | print better_print(response.text) 20 | 21 | 22 | def params_request(): 23 | response = requests.get(build_uri('users'), params={'since': 11}) 24 | print better_print(response.text) 25 | print response.request.headers 26 | print response.url 27 | 28 | 29 | def json_request(): 30 | # response = requests.patch(build_uri('user'), auth=('imoocdemo', 'imoocdemo123'), json={'name': 'babymooc2', 'email': 'hello-world@imooc.org'}) 31 | response = requests.post(build_uri('user/emails'), auth=('imoocdemo', 'imoocdemo123'), json=['helloworld@github.com']) 32 | print better_print(response.text) 33 | print response.request.headers 34 | print response.request.body 35 | print response.status_code 36 | 37 | 38 | def timeout_request(): 39 | try: 40 | response = requests.get(build_uri('user/emails'), timeout=10) 41 | response.raise_for_status() 42 | except exceptions.Timeout as e: 43 | print e.message 44 | except exceptions.HTTPError as e: 45 | print e.message 46 | else: 47 | print response.text 48 | print response.status_code 49 | 50 | 51 | def hard_requests(): 52 | from requests import Request, Session 53 | s = Session() 54 | headers = {'User-Agent': 'fake1.3.4'} 55 | req = Request('GET', build_uri('user/emails'), auth=('imoocdemo', 'imoocdemo123'), headers=headers) 56 | prepped = req.prepare() 57 | print prepped.body 58 | print prepped.headers 59 | 60 | resp = s.send(prepped, timeout=5) 61 | print resp.status_code 62 | print resp.request.headers 63 | print resp.text 64 | 65 | if __name__ == '__main__': 66 | hard_requests() 67 | -------------------------------------------------------------------------------- /ch4/api_response.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | 4 | response = requests.get('https://api.github.com') 5 | print "状态码,具体解释" 6 | print response.status_code, response.reason 7 | print "头部信息" 8 | print response.headers 9 | print "URL 信息" 10 | print response.url 11 | print "redirect 信息" 12 | print response.history 13 | print "耗费时长" 14 | print response.elapsed 15 | print "request 信息" 16 | print response.request.method 17 | 18 | print '----------------------' 19 | 20 | print "编码信息" 21 | print response.encoding 22 | print "消息主体内容: byte" 23 | print response.content, type(response.content) 24 | print "消息主体内容: 解析" 25 | print response.text, type(response.text) 26 | print "消息主体内容" 27 | print response.json(), type(response.json()) 28 | -------------------------------------------------------------------------------- /ch4/hook_response.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | 4 | 5 | def get_key_info(response, *args, **kwargs): 6 | """回调函数 7 | """ 8 | print response.headers['Content-Type'] 9 | 10 | 11 | def main(): 12 | """主程序 13 | """ 14 | requests.get('https://api.github.com', hooks=dict(response=get_key_info)) 15 | 16 | main() 17 | -------------------------------------------------------------------------------- /ch4/img_response.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf -*- 2 | import requests 3 | 4 | 5 | def download_image(): 6 | """demo: 下载图片, 文件 7 | """ 8 | headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'} 9 | url = "http://img3.imgtn.bdimg.com/it/u=2228635891,3833788938&fm=21&gp=0.jpg" 10 | response = requests.get(url, headers=headers, stream=True) 11 | with open('demo.jpg', 'wb') as fd: 12 | for chunk in response.iter_content(128): 13 | fd.write(chunk) 14 | 15 | 16 | def download_image_improved(): 17 | """demo: 下载图片 18 | """ 19 | # 伪造headers信息 20 | headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36'} 21 | # 限定url 22 | url = "http://img3.imgtn.bdimg.com/it/u=2228635891,3833788938&fm=21&gp=0.jpg" 23 | response = requests.get(url, headers=headers, stream=True) 24 | from contextlib import closing 25 | with closing(requests.get(url, headers=headers, stream=True)) as response: 26 | # 打开文件 27 | with open('demo1.jpg', 'wb') as fd: 28 | # 每128写入一次 29 | for chunk in response.iter_content(128): 30 | fd.write(chunk) 31 | 32 | download_image_improved() 33 | -------------------------------------------------------------------------------- /ch5/authen.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import requests 3 | 4 | BASE_URL = 'https://api.github.com' 5 | 6 | 7 | def construct_url(end_point): 8 | return '/'.join([BASE_URL,end_point]) 9 | 10 | 11 | def basic_auth(): 12 | """ 13 | 基本认证 14 | """ 15 | response = requests.get(construct_url('user'),auth=('imoocdemo','imoocdemo123')) 16 | print(response.text) 17 | print(response.request.headers) 18 | 19 | def basic_oauth(): 20 | headers = {'Authorization': 'token dd6322fa6c57a548268453dc245cbcdc352a7811'} 21 | # user/emails 22 | response = requests.get(construct_url('user/emails'),headers=headers) 23 | print(response.request.headers) 24 | print(response.text) 25 | print(response.status_code) 26 | 27 | from requests.auth import AuthBase 28 | 29 | class GithubAuth(AuthBase): 30 | def __init__(self, token): 31 | self.token = token 32 | 33 | def __call__(self, r): 34 | # requests 加headers 35 | r.headers['Authorization'] = ' '.join(['token',self.token]) 36 | return r 37 | 38 | def oauth_advanced(): 39 | auth = GithubAuth('dd6322fa6c57a548268453dc245cbcdc352a7811') 40 | response = requests.get(construct_url('user/emails'),auth=auth) 41 | print(response.text) 42 | # basic_auth() 43 | # basic_oauth() 44 | oauth_advanced() 45 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | click==6.6 2 | decorator==4.0.10 3 | Flask==0.11.1 4 | gunicorn==19.6.0 5 | httpbin==0.4.1 6 | itsdangerous==0.24 7 | Jinja2==2.8 8 | MarkupSafe==0.23 9 | requests==2.10.0 10 | six==1.10.0 11 | Werkzeug==0.11.10 12 | --------------------------------------------------------------------------------