tkyass tkyass - 1 month ago 16
Python Question

Transforming curl command into Python pycurl or requests

Trying to use this curl command, in Python:

curl -k -X POST --data "action=login&username=user&password=pass" https://localhost:8443


I already tried using pycurl as the following:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "https://localhost:8443")
c.setopt(pycurl.POST, 1)
#tried this too
#c.setopt(pycurl.USERPWD, 'user:pass')
c.setopt(c.HTTPHEADER,"action=login&username=user&password=pass" )
c.setopt(c.VERBOSE, True)
c.perform()


I also tried it in requests:

import requests


data = 'action=login&username=user&password=pass'

requests.post('https://localhost:8443', data=data)


but it didn't work. Don't know what I'm missing, any suggestions?

Answer
import requests


data = {'action': 'login', 'username': 'user', 'password': 'pass'}

requests.post('https://localhost:8443', data=data)