如何使用python urllib2发送json数据进行登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4348061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:20:00  来源:igfitidea点击:

How to use python urllib2 to send json data for login

pythonjsonurllib2

提问by richie

I want to use python urllib2 to simulate a login action, I use Fiddler to catch the packets and got that the login action is just an ajax request and the username and password is sent as json data, but I have no idea how to use urllib2 to send json data, help...

我想用python urllib2来模拟登录动作,我用Fiddler抓包,发现登录动作只是一个ajax请求,用户名和密码作为json数据发送,但我不知道如何使用urllib2发送json数据,帮助...

采纳答案by Thomas K

import urllib2
import json
# Whatever structure you need to send goes here:
jdata = json.dumps({"username":"...", "password":"..."})
urllib2.urlopen("http://www.example.com/", jdata)

This assumes you're using HTTP POST to send a simple json object with username and password.

这假设您使用 HTTP POST 发送一个带有用户名和密码的简单 json 对象。

回答by Reto Aebersold

You can specify data upon request:

您可以根据要求指定数据:

import urllib
import urllib2

url = 'http://example.com/login'
values = YOUR_CREDENTIALS_JSON

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()

回答by treecoder

For Python 3.x

对于 Python 3.x

Note the following

请注意以下事项

  • In Python 3.x the urlliband urllib2modules have been combined. The module is named urllib. So, remember that urllibin Python 2.x and urllibin Python 3.x are DIFFERENT modules.

  • The POST data for urllib.request.Requestin Python 3 does NOT accept a string (str) -- you have to pass a bytesobject (or an iterable of bytes)

  • 在 Python 3.x 中,urlliburllib2模块已合并。该模块名为urllib。所以,请记住,urllib在 Python 2.x 和urllibPython 3.x 中是不同的模块。

  • urllib.request.RequestPython 3 中的 POST 数据不接受字符串 ( str) -- 您必须传递一个bytes对象(或可迭代的bytes

Example

例子

pass jsondata with POST in Python 3.x

json在 Python 3.x 中使用 POST传递数据

导入 urllib.request
导入json

json_dict = { 'name': 'some name', 'value': 'some value' }

# 将 json_dict 转换为 JSON
json_data = json.dumps(json_dict)

# 将 str 转换为字节(确保编码正常)
post_data = json_data.encode('utf-8')

# 我们还应该说 JSON 内容类型标头
标题 = {}
headers['Content-Type'] = 'application/json'

# 现在请求一个 url
req = urllib.request.Request(url, post_data, headers)

# 发送请求
res = urllib.request.urlopen(req)

# res 是一个类似文件的对象
# ...

Finally note that you can ONLY send a POST request if you have SOME data to send.

最后请注意,如果您有一些数据要发送,您只能发送 POST 请求。

If you want to do an HTTP POST without sending any data, you should send an empty dict as data.

如果您想在不发送任何数据的情况下执行 HTTP POST,您应该发送一个空的 dict 作为数据。

数据字典 = {}
post_data = json.dumps(data_dict).encode()

req = urllib.request.Request(url, post_data)
res = urllib.request.urlopen(req)

回答by toutpt

You can use the 'requests' python library to achieve this:

您可以使用“请求”python 库来实现此目的:

http://docs.python-requests.org/en/latest/index.html

http://docs.python-requests.org/en/latest/index.html

You will find this example:

你会发现这个例子:

http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests(More complicated POST requests)

http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests(更复杂的 POST 请求)

>>> import requests
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)

It seems python do not set good headers when you are trying to send JSON instead of urlencoded data.

当您尝试发送 JSON 而不是 urlencoded 数据时,python 似乎没有设置好的标头。