Python的POST和GET方法


1.GET方法

get方法是直接将要请求的数据放在url中,下面用httplib和urllib2模拟用户登录。

1)

    #URL地址
    url_Addr = "https://apac-axlprod01-api.com:8081/userLogin?"
    #用户登陆需要传递的参数
    params = urllib.urlencode({'name': user_name, 'password': user_pwd, 'appId': app_Id})
    #我安装的python证书好像有问题,失能校验
    ssl._create_default_https_context = ssl._create_unverified_context
    #将参数和URL组成一个URL
    req = urllib2.Request(url_Addr+params)
    res = urllib2.urlopen(req)
    data = res.read()
    res.close()


2)

    #URL地址
    url_Addr = "apac-axlprod01-api.com:8081"
    #用户登陆需要传递的参数
    params = urllib.urlencode({'name': user_name, 'password': user_pwd, 'appId': app_Id})
    ssl._create_default_https_context = ssl._create_unverified_context
    conn = httplib.HTTPSConnection(url_Addr)
    #将参数和URL组成一个URL
    conn.request("GET", "/userLogin?" + params)
    response = conn.getresponse()
    data = response.read()
    response.close()


2.POST方法

POST方法是直接将要请求的数据放在data或body中,不能放在url中,下面用httplib和urllib2模拟用户登录。

1)

    #URL地址
    url_Addr = "https://apac-axlprod01-api.com:8081/userLogin"
    #用户登陆需要传递的参数
    params = urllib.urlencode({'name': user_name, 'password': user_pwd, 'appId': app_Id})
    ssl._create_default_https_context = ssl._create_unverified_context
    #传入URL和Data
    req = urllib2.Request(url = url_Addr,data = params)
    res = urllib2.urlopen(req)
    data = res.read()
    res.close()


2)

    #URL地址
    url_Addr = "apac-axlprod01-api.com"
    #headers
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    #用户登陆需要传递的参数
    params = urllib.urlencode({'name': user_name, 'password': user_pwd, 'appId': app_Id})
    ssl._create_default_https_context = ssl._create_unverified_context
    conn = httplib.HTTPSConnection(url_Addr,8081)
    #传入URL、body和headers
    conn.request("POST","/userLogin",params,headers)
    response = conn.getresponse()
    data = response.read()
    response.close()


httplib实现了http和https的客户端协议,但是在python中,模块urllib和urllib2对httplib进行了更上层的封装


下面详细介绍httplib提供的常用类型和方法。
httplib.HTTPConnection ( host [ , port [ , strict [ , timeout ]]] )
  HTTPConnection类的构造函数,表示一次与服务器之间的交互,即请求/响应。参数host表示服务器主机, 如:www.csdn.net;port为端口号,默认值为80; 参数strict的 默认值为false, 表示在无法解析服务器返回的状态行时( status line) (比较典型的状态行如: HTTP/1.0 200 OK ),是否抛BadStatusLine 异常;可选参数timeout 表示超时时间。
  HTTPConnection提供的方法:
HTTPConnection.request ( method , url [ , body [ , headers ]] )
  调用request 方法会向服务器发送一次请求,method 表示请求的方法,常用有方法有get 和post和head ;url 表示请求的资源的url ;body 表示提交到服务器的数据,必须是字符串(如果method 是"post" ,则可以把body 理解为html 表单中的数据);headers 表示请求的http 头。
HTTPConnection.getresponse ()
  获取Http 响应。返回的对象是HTTPResponse 的实例,关于HTTPResponse 在下面 会讲解。
HTTPConnection.connect ()
  连接到Http 服务器。
HTTPConnection.close ()
  关闭与服务器的连接。
HTTPConnection.set_debuglevel ( level )
  设置高度的级别。参数level 的默认值为0 ,表示不输出任何调试信息。


httplib.HTTPResponse
  HTTPResponse表示服务器对客户端请求的响应。往往通过调用HTTPConnection.getresponse()来创建,它有如下方法和属性:
HTTPResponse.read([amt])
  获取响应的消息体。如果请求的是一个普通的网页,那么该方法返回的是页面的html。可选参数amt表示从响应流中读取指定字节的数据。
HTTPResponse.getheader(name[, default])
  获取响应头。Name表示头域(header field)名,可选参数default在头域名不存在的情况下作为默认值返回。
HTTPResponse.getheaders()
  以列表的形式返回所有的头信息。
HTTPResponse.msg
  获取所有的响应头信息。
HTTPResponse.version
  获取服务器所使用的http协议版本。11表示http/1.1;10表示http/1.0。
HTTPResponse.status
  获取响应的状态码。如:200表示请求成功。
HTTPResponse.reason
  返回服务器处理请求的结果说明。一般为”OK”


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值