加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

将CURL Post转换为Python请求失败

发布时间:2020-12-13 23:28:45 所属栏目:Linux 来源:网络整理
导读:我无法成功转换并执行curl post命令到 python代码. 卷曲命令 curl -X POST -H "Content-Type:application/json; charset=UTF-8" -d '{"name":joe,"type":22,"summary":"Test"}' http://url 转换代码 import requestsimport json url="http://url"data = {"na
我无法成功转换并执行curl post命令到 python代码.

卷曲命令

curl -X POST -H "Content-Type:application/json; charset=UTF-8" -d '{"name":joe,"type":22,"summary":"Test"}' http://url

转换代码

import requests
import json 

url="http://url"
data = {"name":joe,"summary":"Test"}
headers = {'Content-type': "application/json; charset=utf8"}
response  = requests.post (url,data=json.dumps(data),headers=headers)
print response.text
print response.headers

我没有得到回应,当我从shell手动执行它时工作正常,但是当我执行代码时,没有任何反应,我没有看到错误或任何东西.

解决方法

如果您在您的环境中配置了代理,请在您的会话/请求中定义它.

例如,使用会话:

my_proxies = {  
'http': 'http://myproxy:8080','https': 'https://myproxy:8080'  
}  

session = requests.Session()  
request = requests.Request('POST','http://my.domain.com',data=params_template,headers=req_headers,proxies=my_proxies)  
prepped = session.prepare_request(request)  
response = session.send(prepped)

看文档:
要求http://docs.python-requests.org/en/master/user/quickstart/
会议http://docs.python-requests.org/en/master/user/advanced/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读