1. 程式人生 > >Python 編寫通過post方式提交的接口測試代碼

Python 編寫通過post方式提交的接口測試代碼

urlencode nco 出現 第一次用 python 方式 pen 沒有 iterable

第一次用python編寫程序

是照著視頻裏編寫的

但是期間也出現了很多錯誤

視頻內用了是Python2 版本的

而我的是python 3版本的

寫這兩程序時就發現有很多不同

首先

python3沒有urllib2這個組件

現在是urllib.request

所以在文件前面引入

import urllib.request

urllib2.Request 更換為 urllib.request.Request

urllib2.urlopen 更換為 urllib.request.urlopen

在編碼時一直有報錯

POST data should be bytes or an iterable of bytes. It cannot be of type str.

百度後查找到解決方法

編碼時在後面加上 .encode(encoding=‘UTF8‘)

urllib.parse.urlencode(data).encode(encoding=‘UTF8‘)

以上就解決了這個報錯

Python 編寫通過post方式提交的接口測試代碼