使用 Flask 报错

一、跟着写了一个很简单的Flask应用,直接报错

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "1234"
if __name__=="__main__":
    app.run()

报的错误

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\python\install\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\python\install\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/Python/UseTest.py", line 8, in <module>
    app.run()
  File "C:\Users\youziku\AppData\Roaming\Python\Python36\site-packages\flask\app.py", line 990, in run
    run_simple(host, port, self, **options)
  File "C:\Users\youziku\AppData\Roaming\Python\Python36\site-packages\werkzeug\serving.py", line 1009, in run_simple
    inner()
  File "C:\Users\youziku\AppData\Roaming\Python\Python36\site-packages\werkzeug\serving.py", line 962, in inner
    fd=fd,
  File "C:\Users\youziku\AppData\Roaming\Python\Python36\site-packages\werkzeug\serving.py", line 805, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "C:\Users\youziku\AppData\Roaming\Python\Python36\site-packages\werkzeug\serving.py", line 698, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\socketserver.py", line 453, in __init__
    self.server_bind()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\http\server.py", line 138, in server_bind
    self.server_name = socket.getfqdn(host)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\socket.py", line 673, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 8: invalid start byte

这个雷我踩了两天,现在记录一下,易产生错误的点

1)是代码的编码问题,将页面重新保存设为 utf-8。

2)电脑的名字是中文,以及用户名是中文

3)端口被占用手动指定端口即可

当前两个没用时,不妨指定一下端口

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

 

posted @ 2019-09-05 20:36  太素元清  阅读(944)  评论(0编辑  收藏  举报