data = {"a":1,"b":2}

urllib2

get:

get_data = urllib.urlencode(data)
req_url = URL + '?' + get_data
req = urllib2.Request(url=req_url)
rsp = urllib2.urlopen(req).read()

post:

headers = {"Content-Type":"application/x-www-form-urlencode"}
#headers = {"Content-Type":"application/json"}
#headers = {"Content-Type":"application/form-data"}
一般来说我们使用的headers应该是json,或者正规一点也应该是form-data,但是由于我这里对接的系统使用的是request.getParameter(name)
来获取数据,这东西对给请求倒是一点问题没有,所以这里我们不得不用x-www-form-urlencode req = urllib2.Request(url=URL, data=get_data, headers=headers)
rsp = urllib2.urlopen(req).read()

requests:

post:

  rsp = requests.post(URL, json=data, headers=headers)

或者

  post_data = json.dumps(data, ensure_ascii=False) # 如果直接dumps出错,添加后面这个kwargs

  rsp = requests.post(URL, data=post_data, headers=headers)

python 发送请求的更多相关文章

  1. Python接口测试实战2 - 使用Python发送请求

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

  2. MOOC(3)- python发送请求,返回的json数据被转码

    https://www.cnblogs.com/yoyoketang/p/10339210.html 问题:发送post请求,对post请求返回的json数据格式化,但是返回的结果被转码了 json. ...

  3. Python发送http请求时遇到问题总结

    1.报错信息为“ERROR 'str' object has no attribute 'endwith'”,排查发现endswith方法名写错了,少了s,写成了 'endwith' if inter ...

  4. requests模块--python发送http请求

    requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...

  5. python发送post和get请求

    python发送post和get请求 get请求: 使用get方式时,请求数据直接放在url中. 方法一. import urllib import urllib2 url = "http: ...

  6. python 发送post和get请求

    摘自:http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201231085444250/ 测试用CGI,名字为test.py,放在ap ...

  7. [python网络编程]使用scapy修改源IP发送请求

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  8. python发送HTTP POST请求

    1. 127.0.0.1和0.0.0.0 127.0.0.1是一个回送地址,指本地机,一般用来本机测试使用,使用127.0.0.1启的服务只能在本地机器上访问,使用0.0.0.0启的服务可以在其他机器 ...

  9. python发送GET或POST请求以便干一些趣事

    适合级别:入门,中级 关键字   :python, http, GET, POST, 安全, 模拟, 浏览器, 验证码,图片识别, google 1 此文不是关于黑客或安全话题的! 2 使用脚本程序发 ...

随机推荐

  1. max_length 属性

    错误:漏掉了 max_length  属性 ERRORS:users.UserProfile.image: (fields.E210) Cannot use ImageField because Pi ...

  2. moment.js时间格式化库

    网址:momentjs.cn 主要用来操作时间的格式化.通过发送API请求获取到的数据中:例如[新闻]中的 发布时间,有的时候.请求到的时间,后台没处理过,那么只能前端来操作数据了. moment.j ...

  3. 解决如何通过循环来使用数据库的值设置jsp的select标签的option值

    Select 处的代码: <select name="position"> <span style="white-space:pre"> ...

  4. Vue $ref 的用法

    <div id="app"> <cpn $ref="item"></cpn> <cpn></cpn> ...

  5. 深度学习中的batch、epoch、iteration的含义

    深度学习的优化算法,说白了就是梯度下降.每次的参数更新有两种方式. 第一种,遍历全部数据集算一次损失函数,然后算函数对各个参数的梯度,更新梯度.这种方法每更新一次参数都要把数据集里的所有样本都看一遍, ...

  6. fedora下编译运行java傻瓜入门级教程

    操作步骤: 编译运行环境的搭建 fedora下默认安装为GNU公司的JDK,如下所示 $java -version java version "1.7.0_25" OpenJDK ...

  7. 六、unique_lock

    一.unique_lock取代lock_guard 是个类模板,一般用lock_guard,unique_guard更灵活,效率差一点,内存占用多了一点. 二.unique_lock 的第二个参数 1 ...

  8. 攻防世界 WEB篇

    0x01 ics-06 查看源码发现:index.php 一开始直接用sqlmap跑了下没有发现注入,然后用brupsuite爆破参数 0x02 NewsCenter SQL注入中的POST注入,查阅 ...

  9. 2-prometheus各组件安装

    相关下载: https://prometheus.io/download/https://github.com/prometheus/ 相关文档 https://songjiayang.gitbook ...

  10. Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class

    Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class A well-architecte ...