Heroku应用程序中的Python套接字使请求超时并出现H12错误

时间:2020-07-27 01:27:46

标签: python sockets flask heroku

因此,我使用Flask制作了一个Heroku应用,该应用向服务器发出了套接字请求。端口是一个随机数。主机是我的IP,在这里经过审查。这是我的代码:

Client.py:

   @app.route("/site/<idv>")
   def renderS(idv):
        host = "xxx.xxx.x.x"
        port = int(idv)
        resstr = ""
        try:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                s.connect((host, port))
                s.sendall(bytes(path, 'utf-8'))
                res = s.recv(100024)
                resstr = res.decode("utf-8")
                s.close()
            if (resstr == "404"):
                return render_template("404s.html")
            elif (resstr == "500"):
                return render_template("500s.html")
            else:
                return resstr
        except ConnectionRefusedError as e:
            print(e)
            return render_template("503s.html")

Server.py:

d = data["manifest"]
path = d["all"]["path"]
while True:
     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen()
        conn, addr = s.accept()
        with conn:
            print("Connected by", addr)
            resstr = ""
            while resstr == "":
                res = conn.recv(1024)
                if (len(res) <= 0):
                    break
                resstr += res.decode("utf-8")
            if (not resstr in path):
                conn.sendall(b"404")
                conn.close()
            else:
                try:
                    fn = ""
                    for dv in d["pages"]["page"]:
                        if (dv["path"] == resstr):
                            fn = dv["filename"]
                    with open(pathf+fn) as f:
                        conn.sendall(bytes(f.read(), 'utf-8')) 
                        conn.close()
                except KeyError as e:
                    print("Missing manifest.xml key %s in page %s"%(e[0], resstr))
                    conn.sendall(b"500")
                    conn.close() 

它引发TimeoutError,而Heroku给出H12。

0 个答案:

没有答案