将curl命令转换为Python pycurl或请求

尝试在Python中使用此curl命令:

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

我已经尝试使用pycurl如下:

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()

我也在请求中尝试过:

import requests


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

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

但它不起作用 . 不知道我错过了什么,有什么建议吗?

回答(2)

3 years ago

import requests


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

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

3 years ago

这里是请求翻译

>>> data = 'action=login&username=user&password=pass'
>>> requests.post('https://localhost:8443', data=data)