用Python+Tornado 编写一个个人日记网站(一)

讲道理,用惯了Flask感觉还是不熟悉Djongo,所以先来看一下Tornado(滑稽),记住这是一个最简单的最暴力的网页,毫无细节。

‘Festinatione facit vastum’

环境

Win10 (神奇的我给kali装了Nivdia驱动,崩了)
Python 3.6
Pycharm
Tornado

首先,你需要一个计划

首先无论编写什么程序,无论我们再怎么不正式,起码都要有个计划书,而我的计划比较简单就:

**开发个人日记**-- 新建日记--保存在数据库--信息:1.日期2.天气3.心情4.内容--扩展//未开发5.信纸6.类别7.bgm-- 查询日记--查询--时间--心情--天气

所以现在制定下来了我们的大纲,该怎么实现呢?

[Input]: If i need Python基础 and web前端基础 and 服务器编程基础?
[Output]: Yes…
[Output]: But,it is so f**king EZ.

从HelloWorld开始

你好世界,多么简单的操作:


[Python]
print"Hello,World!"
print("Hello,World!")

val language:String = "Kotlin"
fun main(args: Array<String>) {val HW: String = String(charArrayOf('H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'))printlin(HW)
}

#include <iostream>int main(int argc, const char * argv[]) {std::cout <<"Hello,World"<<std::endl;return 0;
}

emmm,偏题了,break------>

用tornado+Python 编写HelloWorld

打开我们的Pycharm,新建一个工程,我们叫他HelloWorld(因为暴力,工程名就不改了)
新建一个app.py

然后我们需要导入我们的tornado库,但是我们不是什么都需要暂且这样:

from tornado import web,ioloop,httpserver

这个时候我们是不是一筹莫展


一筹莫展 [ yī chóu mò zhǎn ]

筹:筹划、计谋;展:施展。一点计策也施展不出,一点办法也想不出来。筹:筹划、计谋;展:施展。一点计策也施展不出,一点办法也想不出来。筹:筹划、计谋;展:施展。一点计策也施展不出,一点办法也想不出来。
出处出 处出处
清⋅孔尚任《桃花扇》:“下官史可法;日日经略中原;究竟一筹莫展。”清·孔尚任《桃花扇》:“下官史可法;日日经略中原;究竟一筹莫展。”清⋅孔尚任《桃花扇》:“下官史可法;日日经略中原;究竟一筹莫展。”
例句例 句例句
1.面对这道复杂的数学题,他~,不知该从何做起。1. 面对这道复杂的数学题,他~,不知该从何做起。1.面对这道复杂的数学题,他~,不知该从何做起。


当我们不知道干嘛的时候就是英语灵感的时候了,我们要写的是什么?网页应用,对!那他的英文呢?web Application,那我们何不试一下,tornado的开发人员会不会让我们直接明白该怎么用呢?

web.Application

然后利用我们的Pycharm和超链接知识:按住Ctrl 点击 Application,Wow,新大陆(我也是学Kotlin时候才发现的…):

class Application(ReversibleRouter):"""A collection of request handlers that make up a web application.Instances of this class are callable and can be passed directly toHTTPServer to serve the application::application = web.Application([(r"/", MainPageHandler),])http_server = httpserver.HTTPServer(application)http_server.listen(8080)ioloop.IOLoop.current().start()The constructor for this class takes in a list of `~.routing.Rule`objects or tuples of values corresponding to the arguments of`~.routing.Rule` constructor: ``(matcher, target, [target_kwargs], [name])``,the values in square brackets being optional. The default matcher is`~.routing.PathMatches`, so ``(regexp, target)`` tuples can also be usedinstead of ``(PathMatches(regexp), target)``.A common routing target is a `RequestHandler` subclass, but you can alsouse lists of rules as a target, which create a nested routing configuration::application = web.Application([(HostMatches("example.com"), [(r"/", MainPageHandler),(r"/feed", FeedHandler),]),])In addition to this you can use nested `~.routing.Router` instances,`~.httputil.HTTPMessageDelegate` subclasses and callables as routing targets(see `~.routing` module docs for more information).When we receive requests, we iterate over the list in order andinstantiate an instance of the first request class whose regexpmatches the request path. The request class can be specified aseither a class object or a (fully-qualified) name.A dictionary may be passed as the third element (``target_kwargs``)of the tuple, which will be used as keyword arguments to the handler'sconstructor and `~RequestHandler.initialize` method. This patternis used for the `StaticFileHandler` in this example (note that a`StaticFileHandler` can be installed automatically with thestatic_path setting described below)::application = web.Application([(r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),])We support virtual hosts with the `add_handlers` method, which takes ina host regular expression as the first argument::application.add_handlers(r"www\.myhost\.com", [(r"/article/([0-9]+)", ArticleHandler),])If there's no match for the current request's host, then ``default_host``parameter value is matched against host regular expressions.You can serve static files by sending the ``static_path`` settingas a keyword argument. We will serve those files from the``/static/`` URI (this is configurable with the``static_url_prefix`` setting), and we will serve ``/favicon.ico``and ``/robots.txt`` from the same directory.  A custom subclass of`StaticFileHandler` can be specified with the``static_handler_class`` setting... versionchanged:: 4.5Integration with the new `tornado.routing` module."""

妈耶~这么长,但是我们会发现有模板,我们就喜欢模板对吗?

application = web.Application([(r"/", MainPageHandler),])http_server = httpserver.HTTPServer(application)http_server.listen(8080)ioloop.IOLoop.current().start()

怎么用呢?
我们需要定义一个类(MainPageHandler

class MainPageHandler(web.RequestHandler):def get(self, *args, **kwargs):

为什么要这么写呢?看看RequestHandler的注释我们就明白了

"""Base class for HTTP request handlers.Subclasses must define at least one of the methods defined in the"Entry points" section below.
"""
SUPPORTED_METHODS = ("GET", "HEAD", "POST", "DELETE", "PATCH", "PUT","OPTIONS")

然而我们需要的是get数据,那么我们就应该用get方法。(web基础知识)
然后我们需要给他写一个Hello,World让他显示出来,怎么写呢?print肯定不行,那我们就写在短短就好了

class MainPageHandler(web.RequestHandler):def post(self, *args, **kwargs):

[Input]: 接下来呢?这就完了?
[Input]: 给人写信,怎么可能没有地址,没有邮局呢(前提是距离长的像互联网一般)
[Input]: 所以我们要干啥呢?
[Output]: 找人帮忙:

处理、路由、前台

首先我们定义的类(MainPageHandler):就是我们的助手之一,助手a,负责拿到内容

class MainPageHandler(web.RequestHandler):def get(self, *args, **kwargs):

接下来我们需要一个地址,由助手b来跑腿,告诉地址:

application = web.Application([(r"/", MainPageHandler),])

[Input]: 那我们还需要什么呢?
[Output]: 前台,没有助手前台,怎么发送信(这个前台包括邮筒)

前台小助手c:

def main():http_server = httpserver.HTTPServer(application)http_server.listen(8080)ioloop.IOLoop.current().start()

[Input]: 看起来是完了,但是我们写在哪里呢,信纸呢 ?
[Output]: 当然是我们的前端页面

暴力:在当前目录下面新建一个HTML网页:我们叫他index.html,写出下面的html代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>首页</title>
</head>
<body>
<h1>Hello,World</h1>
</body>
</html>

然后我们请寄东西的小助手a,来把信取去:

class MainPageHandler(web.RequestHandler):def get(self, *args, **kwargs):self.render("index.html")

接下来是助手b登场,告诉应该从哪里

application = web.Application([(r"/", MainPageHandler),])

接下来就是我们的前台了,

def main():http_server = httpserver.HTTPServer(application)http_server.listen(8080)ioloop.IOLoop.current().start()

来来来,看看我们的信:

if __name__ == '__main__':main()

开启我们的服务器,就是运行app.py
打开浏览器,去访问 [127.0.0.1:8080](http://127.0.0.1:8080/)

下一篇:[Python]WEB编程–个人日记网站搭建(二)—>如果点不开就是我没写呢,别急么(预计最近更新2019年1月31日12:29:13)

[Python]WEB编程--个人日记网站搭建(一)相关推荐

  1. Web编程——新闻爬虫+网站搭建

    目录 实验内容 实验环境 实验过程 新闻爬虫 请求模块 网页信息提取模块 数据库存储模块 网站搭建 全文搜索 时间热度分析 词云 实验总结 实验内容 爬取新闻网站数据,并搭建网站可视化爬取结果.本次实 ...

  2. asp.net web应用程式的网站搭建及发布

    asp.net web应用程式的网站搭建及发布,记录一下,方便网友们学习及以后查阅 应用环境:Microsoft Visual Studio2013版.互联网信息服务(英语:Internet Info ...

  3. Python Web 编程框架

    目前Python的网络编程框架已经多达几十个,逐个学习它们显然不现实.但这些框架在系统架构和运行环境中有很多共通之处,本文带领读者学习基于Python网络框架开发的常用知识,及目前的4种主流Pytho ...

  4. python: 网络编程及fastapi快速搭建web服务器

    一.网络编程 1.网络编程三要素 ip地址.端口.TCP协议 ip地址 ip地址作用: 根据ip地址能够找到网络中的具体设备(电脑,打印机) ip地址概念: ip地址是网络设备的唯一标识 ip地址分类 ...

  5. Python Web 框架:Django MVC搭建

    为什么80%的码农都做不了架构师?>>>    安装 Django 命令: pip install Django 检验: pythonimport djangodjango.get_ ...

  6. Python核心编程学习日记之错误处理

    第10章 错误处理 异常参数:无论 reason 只包含一个字符串或是由错误编号和字符串组成的元组, 调用 str(reason) 总会返回一个良好可读的错误原因. 这样做其实是调用类的特殊方法 __ ...

  7. python编程基础人邮版答案_《Python Web 编程》(人邮出版社)作业答案下载

    全部作业答案: 链接:https://pan.baidu.com/s/1bm73IFUUfkBl0HHhYovehQ  提取码:bus6 01 Django 基础 链接:https://pan.bai ...

  8. windows7下载python教程-Windows 7下Python Web环境搭建图文教程

    最近想尝试一下在IBM Bluemix上使用Python语言创建Web应用程序,所以需要在本地搭建Python Web的开发测试环境. 关于Python的版本 进入Python的网站,鼠标移到导航条上 ...

  9. 阿里云服务器搭建python web环境_《Python入门》Linux 下 Python Web开发环境搭建笔记-阿里云开发者社区...

    之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境. 我使用的系统是:ubuntu 14.04 server,根据个 ...

最新文章

  1. discuz“附件文件无法保存到远程服务器”故障的解决
  2. python3读取excel文件(xls/xlsx)
  3. Xah Lee Web 李杀网
  4. nosql简答什么是最终一致性_最终一致性 – 如何在NoSQL中维护非规范化一致性?...
  5. jQuery 属性和CSS
  6. ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行
  7. VScode 常用快捷键
  8. 从1亿个ip中找出访问次数最多的IP
  9. opencv 实现等值线_OpenCV图像增强算法实现(直方图均衡化、拉普拉斯、Log、Gamma)...
  10. 火爆Github的刷题攻略,现在配套网站上线了!!力扣刷题网站,刷题路线
  11. 关于sizeof(struct student)的问题
  12. python飞机大战素材黑马_Python基础day12飞机大战(上)
  13. FFmpeg —— 屏幕录像机
  14. rt1021环境搭建(MCUXpresso Config Tools的使用)以及外设配置
  15. 02Windows日志分析
  16. 2020南京大学919经济学原理金融学学硕-上岸
  17. Acm - 隔壁老王买酒问题
  18. Sqlmap免Python版启动器的小修改(根据学习进度逐步更新)
  19. Linux查看系统版号
  20. elasticsearch从入门到入门系列(三)---搜索大汇总

热门文章

  1. php java rsa_java和php实现RSA加密互通-b
  2. python复制excel模板并保留表格样式
  3. JDK9-17开发常用的新特性
  4. Java 之父求职被嫌年纪大
  5. USACO-Charm Bracelet
  6. 一梦江湖——华山快还钱是怎么会事呢?
  7. 软件开发生命周期中的设计阶段_软件的生命周期——测试人员必须了解的事
  8. L0范式、L1范式、L2范式解释通俗版
  9. SpringBoot入门第二天
  10. Java,哈希码以及equals和==的区别(转载)