requests库官方文档地址

         http://docs.python-requests.org/en/master/

作者博客

         http://www.kennethreitz.org

github地址

         https://github.com/requests/requests

环境搭建

基本环境

         python、pip

         注:pip freeze -- 查看当前已经安装的pip包

安装其他必须软件

virtualenv

作用

         初始化一个空环境(也可以不初始化)

         使用 pip / easy_install 是为了能够良好的管理起来你的包,这类似deb之流。

         之所以需要用 VirtualEnv,关键的目的是把你当前开发/生产的 Python 环境和其他的 Python 环境隔离开来。例如你的 Proj1 需要用到 LibA 的版本1.0,而你的 Proj2 需要用到LibA的2.0,如果不加以区分,那么就会可能造成冲突。

         在 VirtualEnv 中,每一个 VirtualEnv 的环境,都是隔离的,需要的包等都是单独的,你可以认为这是一个沙盒(SandBox),这时候 pip / easy_install 等运行时候依赖的 Python 也是隔离的,既 $VENV_HOME/bin/python 而非 /usr/bin/python。

         一般的,yum / apt 安装的会安装在系统的路径中,针对 Python, 则是 Global 的 PYTHONPATH。很难做到隔离。

         而从源代码安装的,一般的会根据你的运行时 Python 命令进行隔离。也就是如果你启用了 VirtualEnv (source $VENV_HOME/bin/activate)后,从源码安装的也会在这个 venv 下。

安装virtualenv

         pip install virtualenv

使用virtualenv初始化当前文件夹

         virtualenv .env

激活当前文件夹的virtualenv

         windows:.env/Script/activate

         linux:source .env/bin/activate

         注:deactivate可以退出虚拟环境

requests库

         pip install requests

httpbin.org

         测试用,由于http://httpbin.org/服务器在美国,所以可以在本地搭建个类似环境测试

         pip install gunicorn httpbin

         gunicorn httpbin:app

         注:windows上无法安装gunicorn

HTTP协议

概念

         HyperText Transfer Protocol超文本传输协议

         The Hypertext Transfer Protocol is a stateless(无状态的), application-level protocol(应用层协议) for distributed(分布式), collaborative(协作式), hepertext information system(超文本信息系统)

显示一次http通信的整个过程

         curl -v https://www.imooc.com/ >/data01/yc_files/http_contact_demo.log

显示分析

        从Request库理解HTTP消息

urllib

概念

         python原生网络库

urllib、urllib2、urllib3的关系

         urllib和urllib2是独立的模块,并没有直接的关系,两者相互结合实现复杂的功能

         urllib和urllib2在python2中才可以使用

         requests库中使用了urllib3(多次请求重复使用一个socket)

urllib和request区别

        从Request库理解HTTP消息

urlib_demo

# -*- coding:utf-8 -*-
import urllib2
import urllib

URL_simple="http://httpbin.org/ip"
URL_get="http://httpbin.org/get"

def urllib_simple_use():
    response = urllib2.urlopen(URL_simple)
    print( '>>>>Response Headers:')
    print( response.info())
    print( '>>>>Response Status Code:')
    print( response.getcode())
    print( '>>>>Response :')
    print( ''.join([line for line in response]))

def urllib_params_use():
    params = urllib.urlencode({'param1':'hello','param2':'world'})
    response = urllib2.urlopen('?'.join([URL_get,params]))
    print( '>>>>Response Headers:')
    print( response.info())
    print( '>>>>Response Status Code:')
    print( response.getcode())
    print( '>>>>Response :')
    print( ''.join([line for line in response]))

if __name__ == '__main__':
    print( '>>>>Urllib Simple Use:')
    urllib_simple_use()
    print( '>>>>Urllib Params Use:')
    urllib_params_use()
urllib simple demo

相关文章:

  • 2022-12-23
  • 2022-02-01
  • 2021-11-10
  • 2021-05-12
  • 2021-11-01
  • 2021-12-11
猜你喜欢
  • 2021-04-19
  • 2021-07-24
  • 2021-11-07
  • 2022-02-20
  • 2021-09-24
相关资源
相似解决方案