gpt4 book ai didi

python - 每次加载网页时出现 RunTimeError

转载 作者:太空宇宙 更新时间:2023-11-04 05:38:13 24 4
gpt4 key购买 nike

今天,当我开始编写我的网页时,它工作正常,但突然我的 css 文件无法工作。我的更改没有更新。读了一点之后,我读到我应该清理我的缓存。我这样做了,之后当我运行我的网页并单击任何按钮时,我会在我的控制台中看到下一个输出:

127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 POST /check-login" - 200 OK
127.0.0.1:50861 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /static/css/swap.css" - 304 Not Modified
RuntimeError('generator raised StopIteration')
127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /" - 200 OK
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/web/httpserver.py", line 239, in __iter__
raise StopIteration()
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1280, in communicate
req.respond()
File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1083, in respond
self.server.gateway(self).respond()
File "/usr/local/lib/python3.7/site-packages/cheroot/wsgi.py", line 145, in respond
for chunk in filter(None, response):
RuntimeError: generator raised StopIteration
127.0.0.1:50860 - - [22/Dec/2019 13:22:20] "HTTP/1.1 GET /static/css/swap.css" - 304 Not Modified
RuntimeError('generator raised StopIteration')
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/web/httpserver.py", line 239, in __iter__
raise StopIteration()
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1280, in communicate
req.respond()
File "/usr/local/lib/python3.7/site-packages/cheroot/server.py", line 1083, in respond
self.server.gateway(self).respond()
File "/usr/local/lib/python3.7/site-packages/cheroot/wsgi.py", line 145, in respond
for chunk in filter(None, response):
RuntimeError: generator raised StopIteration

我的主文件是这样的:

import web   
from Models import RegisterModel, LoginModel

web.config.debug = False

urls = (
'/', 'home',
'/login', 'login',
'/register', 'register',
'/postregistration', 'postRegistration',
'/check-login', 'checkLogin',
'/logout', 'logout'

)

TEMPLATE_PATH = './Views/Templates'
BASE_FILE = 'MainLayout'


app = web.application(urls, globals())

session = web.session.Session(app, web.session.DiskStore("sessions"), initializer={'user': None})
session_data = session._initializer

render = web.template.render(TEMPLATE_PATH, base=BASE_FILE, globals={'session': session_data, 'current_user': session_data["user"]})
# Classes/Routes

class home:
def GET(self):
return render.home()

class login:
def GET(self):
return render.login()

class checkLogin:
def POST(self):
data = web.input()

login_model = LoginModel.LoginModel()
isCorrect = login_model.check_user(data)

if isCorrect:
session_data["user"]=isCorrect
return isCorrect

return "error"

class logout:
def GET(self):
session['user'] = None
session_data['user'] = None
session.kill()
return "success"

class register:
def GET(self):
return render.register()

class postRegistration:
def POST(self):
data = web.input()

reg_model = RegisterModel.RegisterModel()
reg_model.insert_user(data)

return data.username

if __name__ == "__main__":
app.run()

如果您需要有关任何文件的更多信息,请告诉我。

提前致谢。

最佳答案

类似问题here .在 python 3.7 中,StopIteration 的规则发生了变化。

PEP 479 is enabled for all code in Python 3.7, meaning that StopIteration exceptions raised directly or indirectly in coroutines and generators are transformed into RuntimeError exceptions.

你需要像上面的链接一样在错误的地方使用try-except。

编辑

在您的代码中,css 似乎没有更改,因此 304 Not modified 引发 StopIteration。

在../site-packages/web/httpserver.py 的__iter__ 函数中,你会找到这部分。

def __iter__(self):
# ...
try:
path = self.translate_path(self.path)
etag = '"%s"' % os.path.getmtime(path)
client_etag = environ.get("HTTP_IF_NONE_MATCH")
self.send_header("ETag", etag)
if etag == client_etag:
self.send_response(304, "Not Modified")
self.start_response(self.status, self.headers)
return
except OSError:
pass # Probably a 404

在底部添加这个。

except StopIteration:
return

或者使用pass代替return。这将冗长地重写相同的内容。

关于python - 每次加载网页时出现 RunTimeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59444223/

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