在Werkzeug中发送HTTP PUT请求
在我的网页应用中,我使用werkzeug来监听和处理请求。在某个功能中,我需要监听来自A的请求,然后向另一个服务器B发送一个http的put请求,等我从B那里收到响应后,再把这个响应返回给A。
我对werkzeug不是很熟悉,不确定它是否有发送请求的功能,所以我用了httplib来发送请求。
但是我遇到了错误。
这里有很多环节,我想知道以下几点:
- werkzeug是否有发送请求的能力?
- 错误的原因是什么?
感谢任何帮助。
代码:
def complete(self, request):
if request.method == 'GET':
location_id = self.extract_param_value(request,'location_id');
status_code = self.extract_param_value(request,'status_code');
req_url = something;
jsonData = { 'data' : {'status' : status_code, 'id': location_id}};
jsonDataDump = json.dumps(jsonData);
#send request to B
connection = httplib.HTTPConnection(HOST, timeout=5);
body_content = jsonDataDump;
headers = {"Content-type": "application/json", "Accept": "text/plain"};
connection.request('PUT', req_url, body_content, headers); #error occurs here
result = connection.getresponse();
connection.close();
#return response to A
response = Response(status=200);
return response;
错误:
1**.1**.1**.** - - [30/Dec/2011 03:06:57] "GET //complete?location_id=615201308&status_code=LIVE&message=fine HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/ec2-user/y_ws.py", line 381, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python2.6/site-packages/Werkzeug-0.8.1-py2.6.egg/werkzeug/wsgi.py", line 411, in __call__
return self.app(environ, start_response)
File "/home/ec2-user/y_ws.py", line 377, in wsgi_app
response = self.dispatch_request(request);
File "/home/ec2-user/y_ws.py", line 98, in dispatch_request
return getattr(self, endpoint)(request, **values)
File "/home/ec2-user/y_ws.py", line 184, in complete
connection.request('PUT', y_req_url, body_content, headers);
File "/usr/lib64/python2.6/httplib.py", line 914, in request
self._send_request(method, url, body, headers)
File "/usr/lib64/python2.6/httplib.py", line 951, in _send_request
self.endheaders()
File "/usr/lib64/python2.6/httplib.py", line 908, in endheaders
self._send_output()
File "/usr/lib64/python2.6/httplib.py", line 780, in _send_output
self.send(msg)
File "/usr/lib64/python2.6/httplib.py", line 739, in send
self.connect()
File "/usr/lib64/python2.6/httplib.py", line 720, in connect
self.timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
error: [Errno 111] Connection refused
1 个回答
1
- Werkzeug 不是用来发送 HTTP 请求的库,你应该使用 httplib(就像你例子里那样)。
- 用 curl 检查一下从主机机器发出的请求是否成功,这样可以排除网络问题。你遇到的错误是一个通用的网络错误。