[英]How should I send HTTP request with custom method in python
我需要使用自定义方法将 HTTP 请求发送到自定义服务器。 我一直在搜索有关在 python 中执行 curl 命令的信息,并且大部分我发现:
不要那样做!
我需要执行以下 curl 命令:
curl -X MUX -i -H "Connection-Service: API" -H "Service-Address: API" http://172.16.117.40
我一直在尝试使用 python 中的请求库,但没有运气。
我从各种 stackoverflow 答案中构建了这个解决方案:
import httplib, urllib2
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(self.url)
request.add_header('Connection-Service', 'API')
request.add_header('Service-Address', 'API')
request.get_method = lambda: 'MUX'
url = opener.open(request)
url.info().getheader('API')
它作为问题中发布的 curl 命令起作用,我不需要弄乱实际命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.