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

时间:2016-05-23 21:18:12

标签: python curl python-requests 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 个答案:

答案 0 :(得分:1)

import requests


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

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

答案 1 :(得分:0)

这里是请求翻译

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