在werkzeug中发送http put请求

发布于 2024-12-23 19:55:21 字数 2802 浏览 2 评论 0原文

在我的网络应用程序中,我使用 werkzeug 来监听和处理请求。在其中一个功能中,我需要监听请求(例如来自 A 的请求)并向另一台服务器(B)发送 http put 请求,然后在收到 B 的响应后,我向 A 响应一个响应。

我对werkzeug不是很熟悉,不确定它是否有能力发送请求,所以我使用httplib来发送请求。

但我收到错误。

有很多移动部件,我想知道以下内容: 1. werkzeug是否有能力发出请求 2. 错误原因是什么,

感谢任何帮助。

代码:

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

in my web app, I use werkzeug to listen and process requests. In one of the functionalities, I need to listen to request(say from A) and send an http put request to another server (B), then after I get response from B, I respond A an response.

I am not very familiar with werkzeug, and not sure if it has ability to send out requests, so I used httplib to send requests.

But I am getting errors.

There are a lot of moving parts, I am wondering the following:
1. does werkzeug have ability to send out requests
2. what is the cause of the error

Appreciate any help.

Code:

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;

Error:

        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-12-30 19:55:21
  1. Werkzeug 不是用于发出 HTTP 请求的库,请使用 httplib (如您的示例中所示)
  2. 使用curl 检查主机的请求是否成功,以排除网络问题。您收到的错误是一般网络错误。
  1. Werkzeug is not a library for making HTTP requests, use httplib (as in your example)
  2. Check with curl if the request succeeds from the host machine to rule out network issues. The error you are getting is general network error.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文