zoukankan      html  css  js  c++  java
  • python 实现接口测试

      接口的类型有很多,但是我们经常遇见经常用的就get和post两种。这两种有什么区别呢?个人理解主要是表现在安全性方面。

    Python代码POST任意的HTTP数据以及使用Cookie的方法,有需要的朋友可以参考下。

    1)、不使用Cookie时,发送HTTP POST还是非常简单的:

    import urllib2, urllib
     
    data = {'name' : 'www', 'password' : '123456'}
    f = urllib2.urlopen(
            url     = 'http://www.haibian.com/',
            data    = urllib.urlencode(data)
            )
    print f.read()
    

      2)、使用Cookie时, 代码就变得有些复杂了: 

    import urllib2
     
    cookies = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookies)
     
    f = opener.open('http://www.haibian.com')
     
    data = '<root>Hello</root>'
    request = urllib2.Request(
            url     = 'http://www.haibian.com/index/ajax_login',
            headers = {'Content-Type' : 'text/xml'},
            data    = data)
     
    opener.open(request)
    

      注解:
    第一次 open() 是进行登录。服务器返回的 Cookie 被自动保存在 cookies 中,被用在后来的请求。
    第二次 open() 用 POST 方法向服务器发送了 Content-Type=text/xml 的数据。如果你不创建一个 Request,而是直接使用 urlopen() 方法,Python 强制把 Content-Type 改为 application/x-www-form-urlencoded。

  • 相关阅读:
    【程序2】
    【程序1】
    基数排序
    归并排序
    选择排序
    leetcode15
    leetcode221
    leetcode322
    leetcode921
    leetcode922
  • 原文地址:https://www.cnblogs.com/Mushishi_xu/p/4087301.html
Copyright © 2011-2022 走看看