如何解决将curl命令转换为python请求
我正在尝试将curl命令转换为python请求(使用urllib)。这是用于Promotheus推送网关的: https://github.com/prometheus/pushgateway
可以使用curl使用以下方式将数据发布到链接: some_metric {label =“ val1”} 42
示例: CountRec {studentname =“ John”,subject =“ English”,subjectcode =“ DU12345678999”,system =“ pythonETL”} 40
但是我没有得到如何使用python和值40进行发布
以下是我尝试的代码: url ='http:// host:port / metrics / job / jobname / instance / host:port'
myRestRequestObj = urllib.request.Request(url)
myRestRequestObj.add_header('Content-Type','application/json')
myRestRequestObj.get_method = lambda : 'PUT'
myStringJson={'CountRec{"studentname":"John","subject"="English","subjectcode"="DU12345678999","system"="pythonETL"} 40}'
data=urllib.parse.urlencode(myStringJson)
data1 = data.encode('ascii')
res = urllib.request.urlopen(myRestRequestObj,data1)
rest_txt=res.read().decode('utf-8')
return ast.literal_eval(rest_txt)
但是我遇到错误,说它不是有效的非字符串序列或映射对象。
请帮助我解决这个问题:)预先感谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。