gpt4 book ai didi

python - 将我的 IP 地址连接到 flask 服务器时出错 - ubuntu ipython

转载 作者:行者123 更新时间:2023-12-04 19:14:44 24 4
gpt4 key购买 nike

我正在尝试将我自己的服务器与 amazon ec 2 实例创建的 IP 地址连接起来。但是,每当我输入此代码时,都会收到一条错误消息。代码是:

from flask import Flask, request


visitor_counter = 0
webserver = Flask("MyFirstWebServer")

import datetime
import time

@webserver.route('/')
def hello_world():
# stop_server()
return 'Hello James, it is now ' + str(datetime.date.today())


@webserver.route('/testSU')
def hello_su():
# stop_server()
global visitor_counter
visitor_counter += 1
return 'Hello! You are visitor #{i}'.format(i=visitor_counter)

def start_server():
global visitor_counter
visitor_counter = 0
webserver.run(host='aaa.bbb.cc.ddd', port=5000)
return

def stop_server():
shutdown_after_request = request.environ.get('werkzeug.server.shutdown')
shutdown_after_request()
return

start_server()

我收到的错误消息是“OSError:[Errno 99] 无法分配请求的地址”。这是我在运行 python 笔记本时收到的错误消息的详细信息:
OSError                                   Traceback (most recent call last)
<ipython-input-4-2dda11743df7> in <module>()
----> 1 start_server()

<ipython-input-1-dcbe603433f4> in start_server()
30 global visitor_counter
31 visitor_counter = 0
---> 32 webserver.run(host='aaa.bbb.cc.ddd', port=5000)
33 return
34

/usr/local/lib/python3.5/dist-packages/flask/app.py in run(self, host, port, debug, **options)
839 options.setdefault('use_debugger', self.debug)
840 try:
--> 841 run_simple(host, port, self, **options)
842 finally:
843 # reset the first request information if the development server

/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes, request_handler, static_files, passthrough_errors, ssl_context)
706 reloader_type)
707 else:
--> 708 inner()
709
710

/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py in inner()
668 processes, request_handler,
669 passthrough_errors, ssl_context,
--> 670 fd=fd)
671 if fd is None:
672 log_startup(srv.socket)

/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py in make_server(host, port, app, threaded, processes, request_handler, passthrough_errors, ssl_context, fd)
562 else:
563 return BaseWSGIServer(host, port, app, request_handler,
--> 564 passthrough_errors, ssl_context, fd=fd)
565
566

/usr/local/lib/python3.5/dist-packages/werkzeug/serving.py in __init__(self, host, port, app, handler, passthrough_errors, ssl_context, fd)
474 socket.SOCK_STREAM)
475 port = 0
--> 476 HTTPServer.__init__(self, (host, int(port)), handler)
477 self.app = app
478 self.passthrough_errors = passthrough_errors

/usr/lib/python3.5/socketserver.py in __init__(self, server_address, RequestHandlerClass, bind_and_activate)
438 if bind_and_activate:
439 try:
--> 440 self.server_bind()
441 self.server_activate()
442 except:

/usr/lib/python3.5/http/server.py in server_bind(self)
136 def server_bind(self):
137 """Override server_bind to store the server name."""
--> 138 socketserver.TCPServer.server_bind(self)
139 host, port = self.socket.getsockname()[:2]
140 self.server_name = socket.getfqdn(host)

/usr/lib/python3.5/socketserver.py in server_bind(self)
452 if self.allow_reuse_address:
453 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
--> 454 self.socket.bind(self.server_address)
455 self.server_address = self.socket.getsockname()
456

我已经使用终端查看端口 5000 上是否有任何其他服务器/程序打​​开,但没有。我这里有什么问题?

最佳答案

这段代码对我有用 试试看

import os
from flask import Flask


__author__ = 'Rajshekhar Horatti'
app = Flask(__name__)
APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")
def main():
return "hello world"



if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)

主机应该是 host = 0.0.0.0并检查您激活了哪个端口并分配该端口

关于python - 将我的 IP 地址连接到 flask 服务器时出错 - ubuntu ipython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com