python介面自動化2-發送post請求

來源:互聯網
上載者:User

標籤:fiddler   pos   密碼   不用   測試結果   tps   log   com   使用者   

前言

發送post的請求參考例子很簡單,實際遇到的情況卻是很複雜的,首先第一個post請求肯定是登入了,但登入是最難處理的。登入問題解決了,後面都簡單了。

 

一、查看官方文檔

1.學習一個新的模組,其實不用去百度什麼的,直接用help函數就能查看相關注釋和案例內容。

>>import requests

>>help(requests)

2.查看python發送get和post請求的案例

 >>> import requests        >>> r = requests.get(‘https://www.python.org‘)        >>> r.status_code        200        >>> ‘Python is a programming language‘ in r.content        True          ... or POST:             >>> payload = dict(key1=‘value1‘, key2=‘value2‘)        >>> r = requests.post(‘http://httpbin.org/post‘, data=payload)        >>> print(r.text)        {          ...          "form": {            "key2": "value2",            "key1": "value1"          },          ...        }

 

二、發送post請求

1.用上面給的案例,做個簡單修改,發個post請求

2.payload參數是字典類型,傳到如的form裡

 

三、json

1.post的body是json類型,也可以用json參數傳入。

2.先匯入json模組,用dumps方法轉化成json格式。

3.返回結果,傳到data裡

 

四、headers

1.以禪道登入狀態例,類比登陸,這裡需添加要求標頭headers,可以用fiddler抓包

2.講要求標頭寫成字典格式

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",             "Accept": "application/json, text/javascript, */*; q=0.01",             "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",             "Accept-Encoding": "gzip, deflate, br",             "Content-Type": "application/json; charset=utf-8",             "X-Requested-With": "XMLHttpRequest",             "Cookie": "xxx.............",    # 此處cookie省略了             "Connection": "keep-alive"             }

 

五、禪道登入參考代碼

# coding:utf-8 # coding:utf-8 import requests # 禪道host地址 host = "http://127.0.0.1"
def login(s,username,psw):     url = host+"/zentao/user-login.html"
    h = {         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",         "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",         "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",         "Accept-Encoding": "gzip, deflate",         "Referer": host+"/zentao/user-login.html",         # "Cookie":  # 頭部沒登入前不用傳cookie,因為這裡cookie就是保持登入的         "Connection": "keep-alive",         "Content-Type": "application/x-www-form-urlencoded",         }
    body1 = {"account": username,              "password": psw,              "keepLogin[]": "on",              "referer":  host+"/zentao/my/"             }
    # s = requests.session()   不要寫死session
    r1 = s.post(url, data=body1, headers=h)     # return r1.content  # python2的return這個     return r1.content.decode("utf-8")  # python3
def is_login_sucess(res):         if "登入失敗,請檢查您的使用者名稱或密碼是否填寫正確。" in res:                 return False         elif "parent.location=" in res:                 return True         else:                 return False
if __name__ == "__main__":     s = requests.session()     a = login(s, "admin", "e10adc3949ba59abbe56e057f20f883e")     result = is_login_sucess(a)     print("測試結果:%s"%result)

python介面自動化2-發送post請求

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.