1

【python】网络编程

 2 years ago
source link: https://www.guofei.site/2021/04/17/pythonweb.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

【python】网络编程

2021年04月17日

Author: Guofei

文章归类: Python语法 ,文章编号: 1261


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2021/04/17/pythonweb.html

Edit

wsgiref

from wsgiref.simple_server import make_server


def demo_app(environ, start_response):
    start_response("200 OK", [('Content-Type', 'text/html; charset=utf-8')])
    return ['<h1>Hello,web !</h1>'.encode("utf-8"), '<h2>h2</h2>'.encode('utf-8')]


server = make_server("127.0.0.1", 8000, demo_app)

# 只处理一次
# server.handle_request()
# 或者处理无穷次
server.serve_forever()
environ['SERVER_PORT'] # 端口
environ['PATH_INFO'] # 用户输入的网址,例如 '/login'
environ['REMOTE_ADDR'] # 用户ip地址

MVC

  • C:controller
  • V:View,例如html等
  • M:Model,例如数据库
  • T指的是Template,放html模版
  • View,放处理逻辑
  • Model,放数据库配置

Django

pycharm:New Project -> Django ,会生成几个文件模版:

  • manage.py 是启动入口文件
  • settings.py 配置文件
  • url.py 存放 url和函数的对应关系(url路由)
  • wsgi.py 服务端模块

app

一个网站可以有多个app,创建一个app (MTV模式):

python manage.py startapp myapp
  • models.py
  • views.py
  • template:可以放project目录下,也可以放到 app 所在目录下

运行起来

python manage.py runserver 0.0.0.0:9000

参考资料


您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK