使用 python 发送适当的 post 请求
Sending appropriate post requests with python
我正在尝试将 post 请求(包含特定参数和值)发送到 php 服务器(指示检查指定请求是否具有某些参数,在这种情况下它会将值保存到数据库中)。
使用以下 curl 命令,我可以成功地将请求注册到服务器数据库中:
sudo curl -d 'hostname=RegisterTest&ip=127.0.0.1&operatingsystem=testOpsys' -X POST http:///localhost/register.php
这也是我试图用 python 脚本模拟的 curl 命令。 问题是我尝试了各种 python 语法但我无法复制 curl 命令,即使发送了请求(并收到了服务器响应),也不会进行注册。
import requests
url = “http://localhost/register.php”
data = {'hostname=RegisterTest&ip=127.0.0.1&operatingsystem=testOpsys'}
response = requests.post(url, data=data)
print(response)
您需要将数据格式化为字典。
import requests
data = {
'hostname': 'RegisterTest',
'ip': '127.0.0.1',
'operatingsystem': 'testOpsys'
}
response = requests.post('http://localhost/register.php', data=data)
我正在尝试将 post 请求(包含特定参数和值)发送到 php 服务器(指示检查指定请求是否具有某些参数,在这种情况下它会将值保存到数据库中)。
使用以下 curl 命令,我可以成功地将请求注册到服务器数据库中:
sudo curl -d 'hostname=RegisterTest&ip=127.0.0.1&operatingsystem=testOpsys' -X POST http:///localhost/register.php
这也是我试图用 python 脚本模拟的 curl 命令。 问题是我尝试了各种 python 语法但我无法复制 curl 命令,即使发送了请求(并收到了服务器响应),也不会进行注册。
import requests
url = “http://localhost/register.php”
data = {'hostname=RegisterTest&ip=127.0.0.1&operatingsystem=testOpsys'}
response = requests.post(url, data=data)
print(response)
您需要将数据格式化为字典。
import requests
data = {
'hostname': 'RegisterTest',
'ip': '127.0.0.1',
'operatingsystem': 'testOpsys'
}
response = requests.post('http://localhost/register.php', data=data)