小编See*_*Yre的帖子

从django + gunicorn + nginx设置向外部URL发出POST请求

我正在从运行在django + nginx + gunicorn上的Web应用程序内的方法发送一个post请求.在django自己的服务器上执行时,使用相同的代码接收200响应没有问题(使用runserver).

try:
    response = requests.post(post_url, data=some_data)
    if response.status_code == OK and response.content == '':
        logger.info("Request successful")
    else:
        logger.info("Request failed with response({}): {}".format(response.status_code, response.content))
        return response.status_code == OK and response.content == ''
except requests.RequestException as e:
    logger.info("Request failed with exception: {}".format(e.message))
    return False
Run Code Online (Sandbox Code Playgroud)

我在post_url检查了服务器日志,它确实返回了200响应这个数据.但是,当我在gunicorn和nginx后面运行应用程序时,我无法收到响应(但请求正在发送).在try块之后代码卡在第一行,并且gunicorn worker超时(30秒后).

这是post_url上的apache服务器日志:

[14/Sep/2016:13:19:20 +0000] "POST POST_URL_PATH HTTP/1.0" 200 295 "-" "python-requests/2.9.1"
Run Code Online (Sandbox Code Playgroud)

更新:

我忘了提一下,这个请求执行时间不到一秒,所以这不是一个超时问题.配置出了什么问题?我有标准的nginx + gunicorn设置,其中gunicorn被设置为nginx中的proxy_pass.我猜是因为我在nginx代理后面,我应该在从应用程序发送帖子请求时做些不同的事情吗?

django nginx gunicorn

10
推荐指数
1
解决办法
902
查看次数

在 python 中使用 super 与类方法

我正在尝试了解如何进行这项工作。我想使用类方法实例化父类。这段代码给了我一个错误:

class Base(object):
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    @classmethod
    def class_method(cls, a, b, c):
        return cls(a, b, c)

class Child(Base):
    def __init__(self, x, y, z, p):
        super(Child, self).class_method(x, y, z)
        self.p = p

c = Child(1, 2, 3, 10)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

TypeError: __init__() takes at least 5 arguments (4 given)
Run Code Online (Sandbox Code Playgroud)

我也知道原因。这是因为cls变量保存了Child类。因此,当cls(a, b, c)调用时,python 尝试Child使用 4 个参数初始化该类,self, a, b, c。但该类 …

inheritance super python-2.7

6
推荐指数
1
解决办法
2151
查看次数

跨越不同版本的Python __getitem__行为

class A(object):
    def __getitem__(self, item):
        print item

a = A()
a[0:-1]
Run Code Online (Sandbox Code Playgroud)

Windows上的python 2.7.3,2.7.7,2.7.8,3.3.2上的输出:

slice(0, -1, None)
Run Code Online (Sandbox Code Playgroud)

win 32上的python 2.7.6(32位)输出:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute '__len__'
Run Code Online (Sandbox Code Playgroud)

在python 2.7.6中,切片对象尝试获取实例的长度,以便它可以将"-1"转换为实际值.例如,如果实例的长度为10,则输出将为"slice(0,9,None)".

这种行为似乎很奇怪.任何人都可以验证我的观察是否正确吗?如果是,那么这背后是否有任何官方文件?我们如何应对这种行为以支持我们在所有版本的python上的项目?

python magic-methods python-2.7

5
推荐指数
1
解决办法
91
查看次数