简体   繁体   English

远程机器上的 Bottle 服务器

[英]Bottle server on remote machine

I'm trying to test communication between simple bottle front and backends.我正在尝试测试简单的瓶子前端和后端之间的通信。 I'm able to get things to work on localhost on my machine, but I encounter unexpected behavior when I run the same code on a remote azure machine.我能够在我的机器上的 localhost 上运行,但是当我在远程 azure 机器上运行相同的代码时遇到意外行为。

I put three files in one folder, frontend_server.py , backend_server.py , and index.html .我将三个文件放在一个文件夹中,分别是frontend_server.pybackend_server.pyindex.html Locally, if I run the first two servers in a terminal, then navigating to localhost:4040 shows the expected output in the developer console (it prints an object {"test_backend": "test"} ).在本地,如果我在终端中运行前两个服务器,则导航到localhost:4040会在开发人员控制台中显示预期的 output (它会打印 object {"test_backend": "test"} )。 What I would like to do is run the same servers on a remote machine (12.123.123.123, say) and see the same output printed to the developer console when I navigate to http://12.123.123.123:4040 .我想做的是在远程机器上运行相同的服务器(比如 12.123.123.123),当我导航到http://12.123.123.123:4040时,看到相同的 output 打印到开发人员控制台However, in this case, I see POST http://0.0.0.0:8080/test net::ERR_CONNECTION_REFUSED .但是,在这种情况下,我看到POST http://0.0.0.0:8080/test net::ERR_CONNECTION_REFUSED I have also tried changing the POST URL to the remote machine's address -- in this case, the connection times out after about 10 seconds.我还尝试将 POST URL 更改为远程机器的地址——在这种情况下,连接在大约 10 秒后超时。

I suspect there is an issue with the configuration of the remote server.我怀疑远程服务器的配置有问题。 However, I have set inbound rules to * for both ports 4040 and 8080.但是,我已将 4040 和 8080 端口的入站规则设置为*

Here is my backend.这是我的后端。 I thought there might be a CORS issue, so included the after_request hook.我认为可能存在 CORS 问题,因此包括 after_request 挂钩。 It doesn't seem to ever execute, however (no message is printed to the python console).但是,它似乎从未执行过(没有消息打印到 python 控制台)。

#! /usr/bin/env python3
import beaker.middleware
import bottle
import json
app = bottle.Bottle()


@app.hook("after_request")
def enable_cors():
    '''From https://gist.github.com/richard-flosi/3789163

    This globally enables Cross-Origin Resource Sharing (CORS) headers for every response from this server.
    '''
    print("after request")
    bottle.response.headers['Access-Control-Allow-Origin'] = '*'
    bottle.response.headers['Access-Control-Allow-Methods'] = 'PUT, GET, POST, DELETE, OPTIONS'
    bottle.response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'


@app.post("/test")
def test():
    print("this is just a test")
    bottle.response.content_type = "application/json"
    return json.dumps({"test_backend": "test"})

bottle.run(app, host="0.0.0.0", port="8080")

This is my frontend,这是我的前端,

#! /usr/bin/env python
import sys
import os
import bottle
import argparse


@bottle.get("/")
def root_app():
    return bottle.static_file("index.html", root="./")


def main():
    parser = argparse.ArgumentParser(description="Frontend Server")
    parser.add_argument("--host", action="store", dest="host", type=str, help="Host to bind to", default="0.0.0.0")
    parser.add_argument("--port", action="store", dest="port", type=int, help="Port to listen on", default="4040")
    args = parser.parse_args(sys.argv[1:])

    bottle_server_kwargs = {
        "host": args.host,
        "port": args.port,
        "server": "tornado",
        "reloader": False
    }
    bottle.run(**bottle_server_kwargs)
    return


if __name__ == '__main__':
    main()

This is how I make the POST request, in an index.html ,这就是我在index.html中发出 POST 请求的方式,

<!doctype html>
<html>
  <head>
    <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
  </head>
  <body>
  </body>
  <script type="text/javascript">
   $.ajax({
       type: 'POST',
       crossDomain:'true',
       url: "http://0.0.0.0:8080/test",
       success: function(response){
           console.log("success");
           console.log(response);
       }
   });
  </script>
</html>

Any ideas about what might be happening?关于可能发生的事情有什么想法吗?

The issue was solved by @Joran Beasley's comment, add it as the answer to close the question: @Joran Beasley 的评论解决了这个问题,将其添加为关闭问题的答案:

in your ajax call url: http://0.0.0.0:8080/test should be url: http://64.243.2.11:8080/test (or whatever the actual IP of the host is...) 0.0.0.0 just means "listen on all interfaces", its not actually the IP address you are talking to. in your ajax call url: http://0.0.0.0:8080/test should be url: http://64.243.2.11:8080/test (or whatever the actual IP of the host is...) 0.0.0.0 just意思是“监听所有接口”,它实际上不是您正在与之交谈的 IP 地址。

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

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