我们要使用Tornado服务器,我们要首先简单的了解什么是Tornado。
Tornado是使用python编写的一个强大的可扩展的Web服务器,它在处理严峻的网络流量时表现得足够强健,但是却在创建和编写时有着足够的轻量级,并且能够被应用在大量的应用和工具中。
首先我们要明白,文件的使用方法。这是一个文件包的正确放置方法。static这个文件是放置静态的文件的地方,下面在具体的说。templates这个文件夹是放置HTML文件的(在这个程序中)。那个main.py文件是后台的文件也就是说后台文件要和那两个文件夹在同一个级别下。这样文件才可以正常的运行(还有就是在后台文件以及HTML的文件的名字不可是中文,我不知道是我写的原因,还是什么原因)。
然后我们就需要从后台上面运行然后实现与前台的交互。首先我们先看后台的程序: 这是最简单的一个程序,
# -*- coding:utf-8 -*-
import os.path
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render('Yxdl.html') #这是第一个HTML(Yxdl)
class PoemPageHandler(tornado.web.RequestHandler):
def post(self):
noun1 = self.get_argument('noun1')
noun2 = self.get_argument('noun2')
verb = self.get_argument('verb')
noun3 = self.get_argument('noun3')
self.render('poem.html', roads=noun1, wood=noun2, made=verb,
difference=noun3) #这是第二个HTML(poem)
if __name__ == '__main__': #这是这个程序中最为重要的一个部分
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r'/', IndexHandler), (r'/poem', PoemPageHandler)], #这是两个HTML的类型以及打开的方式
template_path=os.path.join(os.path.dirname(__file__), "templates"), #你可以通过向Application类的构造函数传递一个名为templates_path的参数来告诉Tornado从文件系统的一个特定位置提供HTML文件
static_path=os.path.join(os.path.dirname(__file__), "static"), #你可以通过向Application类的构造函数传递一个名为static_path的参数来告诉Tornado从文件系统的一个特定位置提供静态文件
debug=True
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
剩下的就是我们对,前台HTML文件的写入。如下就是Yxdl.html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网易电子邮箱注册</title>
<style type="text/css">
*{margin:0;padding:0;border:0;list-style:none;}
body{background-color:#f1f1f1;font-size:12px;font-family:"微软雅黑","宋体";color:#000;}
.head{width:954px;height:37px;margin:0 auto;padding:19px 0;}
.logo{width:142px;height:37px;background:url(../static/9/images/dot.jpg) repeat-y right center;float:left;}
.logo_text{width:285px;height:37px;background:url(../static/9/images/logo_text.jpg) no-repeat left center;margin-left:22px;float:left;text-indent:-9999px;}
.help{width:100px;height:37px;line-height:37px;float:right;}
.help a:link,.help a:visited{font-size:14px;color:#1d50a2;text-decoration:none;margin-left:5px;float:right;margin-left:8px;}
.help a:hover{text-decoration:underline;}
.nav{width:954px;height:44px;margin:0 auto;}
.nav_left{width:43px;height:44px;background:url(../static/9/images/nav_left.jpg) no-repeat;float:left;}
.nav_center{width:886px;height:44px;line-height:44px;font-size:14px;color:#fff;font-family:"微软雅黑","宋体";padding-left:20px;background:url(9/images/nav_center.jpg) repeat-x;float:left;}
.nav_right{width:5px;height:44px;background:url(../static/9/images/nav_right.jpg) no-repeat;float:left;}
.content{width:950px;overflow:hidden;background-color:#fff;padding-top:47px;border:2px solid #f0f0f0;margin:0 auto;}
dl{width:520px;overflow:hidden;line-height:32px;margin:0 auto;font-size:12px;}
dl dd{width:442px;height:32px;line-height:20px;font-family:"微软雅黑","宋体";color:#999;}
dl dt{width:78px;height:64px;text-align:right;}
dl dt,dl dd{float:left;}
span{color:red;}
input{width:312px;height:25px;line-height:25px;color:#999;font-family:"微软雅黑","宋体";background:url(9/images/input_bg.jpg) repeat-x;border:1px solid #999;}
.one .input_1{width:195px;height:25px;line-height:25px;}
strong{color:#000;}
select{border:1px solid #999;width:96px;}
.yz{width:442px;height:25px;padding:2px 0;}
.yz_input,img{float:left;margin-right:3px;}
.last{width:78px;height:175px;}
.agree_che{width:12px;height:12px;margin-right:5px;}
.agree{color:#999;}
a:link,a:visited{color:#1d50a2;text-decoration:none;}
a:hover{text-decoration:underline;}
.submit{width:104px;height:37px;background:url(../static/9/images/button.jpg) no-repeat;margin:40px 0 45px 0;cursor:pointer;}
.footer{width:954px;height:50px;line-height:50px;text-align:center;margin:0 auto;color:#999;}
.footer span{color:#999;;margin:0 8px;}
.footer a:link,.footer a:visited{color:#999;margin:0 4px;text-decoration:none;}
.footer a:hover{color:#1d50a2;text-decoration:underline;}
</style>
</head>
<body>
<div class="head">
<div class="logo"><a href="#"><img src="../static/9/images/wy_logo.jpg" /></a></div>
<div class="logo_text">中国第一大电子邮件服务商</div>
<div class="help"><a href="#">帮助</a><a href="#">反馈意见</a></div>
</div>
<div class="nav">
<div class="nav_left"></div>
<div class="nav_center">欢迎注册网易免费邮箱!您可以选择注册163、126、QQ、yeah.net四大免费邮箱!</div>
<div class="nav_right"></div>
</div>
<div class="content">
<dl class="one" name="noun1">
<dt><span>*</span>邮件地址:</dt>
<dd class="input_dd">
<input class="input_1"type="text" value="建议输入手机号注册" />
<strong>@</strong>
<select>
<option>163.com</option>
<option>126.com</option>
<option>yeah.net</option>
<option>qq.com</option>
</select>
</dd>
<dd>6-18个字符,可使用字母、数字、下划线。推荐以手机号直接注册。</dd>
</dl>
<dl name="noun2">
<dt><span>*</span>密码:</dt>
<dd>
<input class="input_1" type="password" />
</dd>
<dd>6-1个字符,区分大小写。</dd>
</dl>
<dl name="noun3">
<dt><span>*</span>确认密码:</dt>
<dd>
<input type="password" />
</dd>
<dd>请在输入密码。</dd>
</dl>
<dl name="noun4">
<dt>手机号码:</dt>
<dd>
<input type="text" />
</dd>
<dd>密码遗忘或者是被盗时,可通过手机短信取回密码。</dd>
</dl>
<dl name="noun5">
<dt class="last"><span>*</span>验证码:</dt>
<dd>
<div class="yz"><input class="yz_input" type="text" /><img src="../static/9/images/yz.jpg" /></div>
</dd>
<dd>请输入图片中的字符,不区分大小写。<a href="#">看不清?换张图片</a></dd>
<dd><input class="agree_che" type="checkbox" checked="check" />
<span class="agree">同意“</span><a href="#">服务条款</a>
<span class="agree">”和“</span><a href="#">隐私权保护和个人信息利用政策</a>”
</dd>
<dd><a href="http://localhost:63342/Tornado/登录后台加前台/templates/poem.html?_ijt=6g2n4aukathl9ms9rv0hnrcp3b"><input type="submit" class="button" /></a></dd>
</dl>
</div>
<div class="footer">
<a href="#">关于网易</a><a href="#">关于网易免费邮箱</a><a href="#">邮箱官方博客</a><a href="#">客户服务</a><a href="#">隐私政策</a>
<span>|</span><a href="#">网页公司版权所有</a>©2000-2018
</div>
</body>
</html>
这个是poem.html文件:
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head><title>生存还是死亡</title></head>
<body>
<h1></h1>
<p>这是账号{{roads}} 这是密码 {{wood}}, and I—<br>
I took the one less travelled by,<br>
确认密码{{made}} 这是最后的 {{difference}}.</p>
</body>
</html>
这样才会是一个完整的文件的形式。这样才可以进行两个HTML文件以及后台之间的交互。
但是这只是一个简化版的登录程序。登录时可能发生的各种情况比:弹窗、报错、数据库的调用等等
我还没有加上,等过几天学完了,在加吧。