Python 编写通过post方式提交的接口测试代码

第一次用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')

 

以上就解决了这个报错

 

posted @ 2017-06-19 18:30  小游y  阅读(1627)  评论(0编辑  收藏  举报