1. json格式的post请求

关键部分加粗显示了,主要是post数据的编码方式以及请求头的Content-type

#coding=utf8
import json
import gzip
import msgpack
import urllib
import urllib2
import tarfile def request():
try:
url = "http://10.11.12.13/abc/def"
values = {"a":1, "b":2, "c":3, "d":4}
data = json.JSONEncoder().encode(values)
print data
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent' : user_agent, 'Content-type':"application/json"}
req = urllib2.Request(url, data=data, headers=headers)
res_data = urllib2.urlopen(req)
res = res_data.read() print "response:" + str(len(res))
if res_data.getcode() == 200:
return res
except urllib2.HTTPError, err:
print(err.code)
print(err.read())
raise

相应的flask获取数据方式:

req_data = request.get_data()
json_data = json.loads(req_data)
a = json_data['a']
b = json_data['b']

2. 浏览器的原生form表单格式,对应 Content-type:application/x-www-form-urlencoded

#coding=utf8
import json
import gzip
import msgpack
import urllib
import urllib2
import tarfile def request():
try:
url = "http://10.11.12.13/abc/def"
values = {"a":1, "b":2, "c":3, "d":4}
data = urllib.urlencode(values)
print data
req = urllib2.Request(url, data)
res_data = urllib2.urlopen(req)
res = res_data.read() print "response:" + str(len(res))
if res_data.getcode() == 200:
return res
except urllib2.HTTPError, err:
print(err.code)
print(err.read())
raise

可以看到,主要区别是数据编码方式不同。

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

  1. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...

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

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

  3. python发送网络请求

    1.使用urllib模块(使用不方便,建议使用第二种) get请求: res = urlopen(url) from urllib.request import urlopen url = 'http ...

  4. python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息

    背景介绍: 发送搜索请求时,需要用到登录接口返回值中的token值 代码实现: 登录代码: 搜索接口:

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

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

  6. python发送post请求

    urllib2.urlopen() urlib2是使用各种协议完成打开url的一个扩展包.最简单的使用方式是调用urlopen方法,比如 def urlopen(url, data=None, tim ...

  7. python 发送POST请求

    #博客地址:https://blog.csdn.net/qq_36374896 import urllib.request import urllib.parse url = "http:/ ...

  8. 转载:python发送HTTP请求

    1. [代码]GET 方法 import httplib #----------------------------- conn = httplib.HTTPConnection("www. ...

  9. python 发送GET请求

    # #博客地址:https://blog.csdn.net/qq_36374896 # 特点:把数据拼接到请求路径的后面传递给服务器 # # 优点:速度快 # # 缺点:承载的数据量小,不安全 imp ...

  10. python接口自动化(八)--发送post请求的接口(详解)

    简介 上篇介绍完发送get请求的接口,大家必然联想到发送post请求的接口也不会太难,被聪明的你又猜到了.答案是对的,虽然发送post请求的参考例子很简单,但是实际遇到的情况却是很复杂的,因为所有系统 ...

随机推荐

  1. 15 个 Android 通用流行框架大全(转)

    1. 缓存 DiskLruCache    Java实现基于LRU的磁盘缓存 2.图片加载 Android Universal Image Loader 一个强大的加载,缓存,展示图片的库 Picas ...

  2. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  3. smarty使用

    smarty-牛刀小试 smarty 初识 官网 http://www.smarty.net/ Smarty is a template engine for PHP(PHP模板引擎) smarty使 ...

  4. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

  5. react

    package.json ENOSPC 文件监控增加一些 echo fs.inotify.max_user_watches=582222 | sudo tee -a /etc/sysctl.conf ...

  6. JS 小数的常用处理方法

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...

  7. Table 表单

    <style> table th { white-space: nowrap; } .chk { white-space: nowrap; } </style> <tab ...

  8. Oracle 环境变量NLS_LANG

    NLS_LANG是一个环境变量,用于定义语言,地域以及字符集属性.对于非英语的字符集,NLS_LANG的设置就非常重要.NLS:'National Language Support (NLS)' 当我 ...

  9. 移动端Web开发调试之Chrome远程调试(Remote Debugging)

    比如手机钉钉调试页面,下面是一位同学整理的链接: http://blog.csdn.net/freshlover/article/details/42528643/ 如果inspect 后,一直空白, ...

  10. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...