本文介绍了进行GET而不是POST请求的python-requests的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每天都有一个cron来处理我的应用程序中的一些重复发生的事件,并且我不时注意到一个奇怪的错误出现在日志中.除其他事项外,cron会对一些代码进行验证,并且它使用在同一服务器上运行的webapp,因此该验证请求是通过POST请求发出的,其中包含一些数据.

I have a daily cron which handles some of the recurring events at my app, and from time to time I notice one weird error that pops up in logs. The cron, among other things, does a validation of some codes, and it uses the webapp running on the same server, so the validation request is made via POST request with some data.

url = 'https://example.com/validate/'
payload = {'pin': pin, 'sku': sku, 'phone': phone, 'AR': True}
validation_post = requests.post(url, data=payload)

因此,这是实际的请求,我记录了响应.有时,在最近达到请求的50%的情况下,响应都包含来自nginx的以下消息:

So, this makes the actual request and I log the response. From time to time, and recently up to 50% of the request, the response contains the following message from nginx:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method GET is not allowed for the requested URL.</p>

因此,实际请求是使用 GET 方法而不是代码中指示的 POST 发出的.在nginx access.log中,我可以看到该条目:

So, the actual request was made using the GET method, not the POST as it was instructed in the code. In the nginx access.log I can see that entry:

123.123.123.123 - - [18/Feb/2015:12:26:50 -0500] "GET /validate/ HTTP/1.1" 405 182 "-" "python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-37-generic"

该应用程序的uwsgi日志显示了类似的内容:

And the uwsgi log for the app shows the similar thing:

[pid: 6888|app: 0|req: 1589/58763] 123.123.123.123 () {40 vars in 613 bytes} [Mon Apr  6 11:42:41 2015] GET /validate/ => generated 182 bytes in 1 msecs (HTTP/1.1 405) 4 headers in 234 bytes (1 switches on core 0)

因此,所有内容都指出,实际请求不是使用 POST 发出的.处理此代码的应用程序路线很简单,摘录如下:@ app.route('/validate/',methods = ['POST'])@login_required

So, everything points out that the actual request was not made using the POST. The app route that handles this code is simple, and this is an excerpt:@app.route('/validate/', methods=['POST'])@login_required

def validate():
    if isinstance(current_user.user, Sales):
        try:
            #do the stuff here
        except Exception, e:
            app.logger.exception(str(e))
            return 0
    abort(403)

应用程序路由可能会失败,并且try块中有一些returns,但是即使这些失败或发生意外,也没有任何东西可以在此块中引发405错误代码,只有403,这很少发生,因为我是从cron手动构建并登录用户的.

The app route can fail, and there are some returns inside the try block, but even if those fails or there is an expcetion, there is nothing that could raise the 405 error code in this block, only 403 which rarely happens since I construct and login the user manually from the cron.

我在此处找到了类似的东西,但是这里的灵魂是那里是从HTTP到HTTPS版本的重定向,我在服务器中也存在该重定向,但是正在发出请求的URL中包含HTTPS,所以我怀疑这是原因.

I have found similar thing here but the soultion there was that there was a redirect from HTTP to HTTPS version, and I also have that redirect present in the server, but the URL the request is being made has the HTTPS in it, so I doubt this is the cause.

我正在其上运行的堆栈是uwsgi + nginx + flask.谁能看到是什么原因造成的?重复一遍,它并非总是会发生,因此有时它会按预期运行,有时则不会.我最近从apachemod_wsgi迁移到了这个新堆栈,从那时起,我开始处理这个错误.我想不起在apache环境中看到过它.

The stack I am running this on is uwsgi+nginx+flask. Can anyone see what might be causing this? To repeat, its not happening always, so sometimes its working as expected, sometimes not. I recently migrated from apache and mod_wsgi to this new stack and from that point I have started encontering this error; can't recally ever seeing it on apache environment.

谢谢!

推荐答案

我们唯一将POST请求更改为GET的方法是在处理重定向时.根据重定向代码,我们将更改请求方法.如果要确保我们不遵循重定向,则需要传递allow_redirects=False.也就是说,您需要弄清楚应用程序为何生成重定向(包括重定向到HTTP或其他域,或使用特定状态代码的原因).

The only time we ever change a POST request to a GET is when we're handling a redirect. Depending on the redirect code, we will change the request method. If you want to be sure that we don't follow redirects, you need to pass allow_redirects=False. That said, you need to figure out why your application is generating redirects (including if it's redirecting to HTTP or to a different domain, or using a specific status code).

这篇关于进行GET而不是POST请求的python-requests的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:50
查看更多