[英]Python webpage login error
我正在嘗試使用python和cherrypy創建一種Web服務器。
我希望將htmls放入單獨的文件中,並將其嵌入到我的python腳本中。 我曾經這樣做的代碼是。
@cherrypy.expose
def welcome(self, loginAttempt = None):
""" Prompt the user with a login form. The form will be submitted to /signin
as a POST request, with arguments "username", "password" and "signin"
Dispaly a login error above the form if there has been one attempted login already.
"""
#Debugging Process Check
print "welcome method called with loggedIn = %s" % (loginAttempt)
if loginAttempt == '1':
""" If the user has attempted to login once, return the original login page
with a error message"""
page = get_file("loginPageE.html")
return page
else:
page = """
<form action='/signin' method='post'>
Username: <input type='text' name='username' /> <br />
Password: <input type='password' name='password' />
<input type='submit' name='signin' value='Sign In'/>
</form>
"""
return page
loginPageE.html在哪里
<html>
<head>
<title>Failed Login Page</title>
</head>
<body>
<!-- header-wrap -->
<div id="header-wrap">
<header>
<hgroup>
<h1><a href="loginPageE.html">Acebook</a></h1>
<h3>Not Just Another Social Networking Site</h3>
</hgroup>
<ul>
<form action='/signin' method='post'>
Username: <input type='text' name='username' />
Password: <input type='password' name='password' />
<input type='submit' name='signin' value='Sign In'/>
</form>
</ul>
</header>
</div>
</body>
</html>
但是我不斷收到一條錯誤消息,內容為
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
cherrypy.response.body = self.handler()
File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
return self.callable(*self.args, **self.kwargs)
File "proj1base.py", line 74, in welcome
page = get_file("loginPageE.html")
NameError: global name 'get_file' is not defined
我想知道是否有人可以幫忙?
提前致謝
好吧,從錯誤中可以明顯看出,python不知道get_file()
函數是什么。 您確定在welcome()
函數中調用此函數的時間點已經定義了get_file()
嗎?
get_file不是標准的Python函數之一,因此它必須是您以前擁有的自定義函數。 您可以創建一個簡單的函數來讀取文件並以字符串形式返回其內容,如下所示:
def get_file(path):
f = open(path, 'r')
output = f.read()
f.close()
return output
您可以在http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files上閱讀Python文件管理
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.