简体   繁体   中英

Sending appropriate post requests with python

I am trying to send a post request (that contains specific parameters and values) to a php server (that is instructed to check if the specified request has certain parameters and in that case it will save the values into a database).

With the following curl command i am successfully able to register the request into the server database:

sudo curl -d 'hostname=RegisterTest&ip=127.0.0.1&operatingsystem=testOpsys' -X POST http://localhost/register.php

This is also the curl command that i am trying to simulate with a python script. The problem is that i have tried various python syntaxes but i couldn't replicate the curl command and even if the request is sent (and server response is received) the registration will not take place.

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)

You need to format the data as a dictionary.

import requests

data = {
  'hostname': 'RegisterTest',
  'ip': '127.0.0.1',
  'operatingsystem': 'testOpsys'
}

response = requests.post('http://localhost/register.php', data=data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM