简体   繁体   English

请求/ URLlib在Flask / Apache / mod_wsgi / Windows中不起作用

[英]Requests/urllib not working in Flask/Apache/mod_wsgi/Windows

I have a Flask app with code that processes data coming from a request to another web app hosted on a different server, and it works just fine in development, furthermore, the library that processes the request can be called and used perfectly fine from python in our Windows server... However when the library is called by the webapp in production using mod_wsgi it refuses to work, requests made by the server just... time out. 我有一个Flask应用,该应用的代码可处理来自请求的数据,该数据处理来自另一台服务器上托管的另一个Web应用,并且在开发中也可以正常工作,此外,可以从python中调用并完美地处理该请求的库我们的Windows服务器...但是,当webapp在生产中使用mod_wsgi调用该库时,该库将无法工作,服务器发出的请求只是...超时。

I have tried everything from moving my code to the file it's used in, to switching from requests to urllib... nothing, so long as they're made from mod_wsgi all requests I make time out. 我已经尝试了所有方法,从将代码移动到使用的文件中,再到从请求切换到urllib……什么都没有,只要它们是由mod_wsgi发出的,我就会超时。 Why is that? 这是为什么? is it some weird apache configuration thing that I'm unaware of? 我不知道这是一些奇怪的Apache配置吗?

I'm posting the library below (sorry I have to censor it up a bit, but I promise it works ) 我在下面发布了库(对不起,我必须对其进行审查,但我保证它可以工作

import requests
import re


class CannotAccessServerException(Exception):
    pass

class ServerItemNotFoundException(Exception):
    pass


class Service():
    REQUEST_URL = "http://server-ip/url?query={query}&toexcel=csv"

    @classmethod
    def fetch_info(cls, query):
        # Get Approximate matches
        try:
            server_request = requests.get(cls.REQUEST_URL.format(query = query), timeout = 30).content
        except:
            raise CannotAccessServerException

        # If you're getting ServerItemNotFoundException or funny values consistently maybe the server has changed their tables.
        server_regex = re.compile('^([\d\-]+);[\d\-]+;[\d\-]+;[\d\-]+;[\d\-]+;[\-"\w]+;[\w"\-]+;{query};[\w"\-]+;[\w"\-]+;[\w"\-]+;[\w"\-]+;[\w\s:"\-]+;[\w\s"\-]+;[\d\-]+;[\d\-]+;[\d\-]+;([\w\-]+);[\w\s"\-]+;[\w\-]+;[\w\s"\-]+;[\d\-]+;[\d\-]+;[\d\-]+;([\w\-]+);[\d\-]+;[\d\-]+;[\w\-]+;[\w\-]+;[\w\-]+;[\w\-]+;[\w\s"\-]+$'.format(query = query), re.MULTILINE)

        server_exact_match = server_regex.search(server_request.decode())

        if server_exact_match is None:
            raise ServerItemNotFoundException


        result_json = {
            "retrieved1": server_exact_match.group(1),
            "retrieved2": server_exact_match.group(2),
            "retrieved3": server_exact_match.group(3)
        }

        return result_json


if __name__ == '__main__':
    print(Service.fetch_info(99999))

PS: I know it times out because one of the things I tried was capturing the error raised by requests.get and returning its repr esentation. PS:我知道这时候,因为我试过被捕获由requests.get引发的错误,并返回它的再版 esentation的事情之一。

In case anybody's wondering, after lots of research, trying to run my module as a subprocess, and all kinds of experiments, I had to resort to replicating the entirety of the dataset I needed to query from the remote server to my database with a weekly crontab task and then querying that. 如果有人想知道,经过大量研究,试图将我的模块作为子流程运行,以及进行各种实验,我不得不每周一次将需要从远程服务器查询到的全部数据集复制到我的数据库中crontab任务,然后进行查询。

So... Yeah, I don't have a solution, to be frank, or an explanation of why this happens. 所以...坦白地说,我没有解决方案,也没有解释为什么会发生这种情况。 But if this is happening to you, your best bet might sadly be replicating the entire dataset on your server. 但是,如果发生这种情况,您最好的选择可能是复制服务器上的整个数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM