import urllib2
import requests
import json
gh_url = 'https://api.github.com'
gh_user = 'gaufung'
gh_pw = 'gaofenggit123'
req = urllib2.Request(gh_url)

password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, gh_url, gh_user, gh_pw)

auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)

urllib2.install_opener(opener)

handler = urllib2.urlopen(req)

if handler.getcode() == requests.codes.ok:
    text = handler.read()
    d_text = json.loads(text)
    for k, v in d_text.items():
        print k, v

import requests
import json
gh_url = 'https://api.github.com'
gh_user = 'gaufung'
gh_pw = 'gaofenggit123'
r = requests.get(gh_url,auth=(gh_user,gh_pw))
if r.status_code == requests.codes.ok:
    for k, v in r.json().items():
        print k,v

"""
Explanation: Python Request 库入门
1 urllib2 和 Request对比
Get请求至https://api.github.com/
End of explanation
"""


import requests
cs_url = 'http://httpbin.org'
r = requests.get("%s/%s" % (cs_url, 'get'))
r = requests.post("%s/%s" % (cs_url, 'post'))
r = requests.put("%s/%s" % (cs_url, 'put'))
r = requests.delete("%s/%s" % (cs_url, 'delete'))
r = requests.patch("%s/%s" % (cs_url, 'patch'))
r = requests.options("%s/%s" % (cs_url, 'get'))

"""
Explanation: 2 基本用法
End of explanation
"""


import requests
cs_url =  'https://www.so.com/s'
param = {'ie':'utf-8','q':'query'}
r = requests.get(cs_url,params = param)
print r.url

"""
Explanation: 3 URL 传参

https://encrypted.google.com/search?q=hello 
<协议>://<域名>/<接口>?<键1>=<值1>&<键2>=<值2>  

requests 库提供的 HTTP 方法，都提供了名为 params 的参数。这个参数可以接受一个 Python 字典，并自动格式化为上述格式。
End of explanation
"""


import requests
cs_url = 'https://www.zhihu.com'
r = requests.get(cs_url,timeout=100)

"""
Explanation: 4 设置超时
requests 的超时设置以秒为单位。例如，对请求加参数 timeout = 5 即可设置超时为 5 秒
End of explanation
"""


import requests

cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url)
print r.content

"""
Explanation: 5 请求头
End of explanation
"""


import requests

my_headers = {'User-Agent' : 'From Liam Huang', 'Accept-Encoding' : 'gzip'}
cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url, headers = my_headers)
print r.content

"""
Explanation: 通常我们比较关注其中的 User-Agent 和 Accept-Encoding。如果我们要修改 HTTP 头中的这两项内容，只需要将一个合适的字典参数传给 headers 即可。
End of explanation
"""


import requests

cs_url = 'http://httpbin.org/get'
r = requests.get (cs_url)
print r.headers

"""
Explanation: 6 响应头
End of explanation
"""


import requests

cs_url = 'https://www.zhihu.com'
r = requests.get (cs_url)

if r.status_code == requests.codes.ok:
    print r.content

"""
Explanation: 7 响应内容
长期以来，互联网都存在带宽有限的情况。因此，网络上传输的数据，很多情况下都是经过压缩的。经由 requests 发送的请求，当收到的响应内容经过 gzip 或 deflate 压缩时，requests 会自动为我们解包。我们可以用 Response.content 来获得以字节形式返回的相应内容。
End of explanation
"""


import requests
from PIL import Image
from StringIO import StringIO

cs_url = 'http://liam0205.me/uploads/avatar/avatar-2.jpg'
r = requests.get (cs_url)

if r.status_code == requests.codes.ok:
    Image.open(StringIO(r.content)).show()

"""
Explanation: 如果相应内容不是文本，而是二进制数据（比如图片），则需要进行响应的解码
End of explanation
"""


import requests

cs_url = 'https://www.zhihu.com'
r = requests.get (cs_url,auth=('gaofengcumt@126.com','gaofengcumt'))

if r.status_code == requests.codes.ok:
    print r.text
else:
    print 'bad request'

"""
Explanation: 文本模式解码
End of explanation
"""


import requests

cs_url   = 'http://ip.taobao.com/service/getIpInfo.php'
my_param = {'ip':'8.8.8.8'}

r = requests.get(cs_url, params = my_param)

print r.json()['data']['country'].encode('utf-8')

"""
Explanation: 8 反序列化 JSON 数据
End of explanation
"""


