Skip to content

Latest commit

 

History

History
184 lines (143 loc) · 3.47 KB

File metadata and controls

184 lines (143 loc) · 3.47 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0579
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在: 

     个人网站: `https://oeasy.org` 
     蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
     GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
     Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

前端和后端

我们来回顾一下 😌

  • 上次应用了 请求和响应
    • 请求 就是 客户发出的 文字
    • 响应 就是 翻译官 翻译好的 文字
    • 后台就是翻译官
  • 可以用表单
    • 来做个webshell吗?

打造静态页面

mkdir my_web_shell
cd my_app
mkdir static
vi static/pre.html        
tree

图片描述

  • 构造网页
<html lang="zh">
	<head>
        <title>webshell</title>
	</head>
	<body>
        <form method="POST" id="my_form" action="../excute">
            <input type="text" id="command" name="command">
            <input type="submit" value="go"/>
        </form>
	</body>
</html>
  • 注意此时 form 提交的方法为
    • POST

构建响应路由

vi app.py
  • 编辑主程序
from flask import Flask
from flask import request
import os

app = Flask(__name__)

@app.route('/')
def index():
    return "hello!"

@app.route('/excute',methods=['POST',"GET"])
def excute():
    s = request.form["command"]
    print(s)
    os.system(s)
    return s

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

运行结果

  • 后台可以看到运行结果

图片描述

  • 怎样将命令结果输出到页面呢?

修改处理函数


@app.route('/excute',methods=['POST',"GET"])
def excute():
    s = request.form["command"]
    print(s)
    os.system(s + "> result.txt")
    with open("result.txt") as f:
        l = f.readlines()
    return l

  • 页面输出结果

图片描述

  • 需要的是网页的形式

再修改代码

@app.route('/excute',methods=['POST',"GET"])
def excute():
    s = request.form["command"]
    print(s)
    os.system(s + "> result.txt")
    with open("result.txt") as f:
        l = f.readlines()
    result = ""
    for line in l:
        line = line.replace("\n","<br>")
        result += line
    return result
  • 把文档中的换行(\n)
    • 替换为网页中的换行(
      )

结果

  • 提交ls -l

图片描述

  • 如何直接回到pre.htmll

再修改代码

@app.route('/excute',methods=['POST',"GET"])
def excute():
    s = request.form["command"]
    os.system(s + "> result.txt")
    with open("result.txt") as f:
        l = f.readlines()
    result = """
    <html lang="zh">
	<head>
        <title>webshell</title>
	</head>
	<body>
        <form method="POST" id="my_form" action="../excute">
            <input type="text" id="command" name="command">
            <input type="submit" value="go"/>
        </form>
    """
    for line in l:
        line = line.replace("\n","<br>")
        result += line
    return result
  • 试着用shell命令
    • 从浏览器 发请求
    • 把服务给停掉...
    • 😂

总结 🤨

  • 这次制作了
    • webshell
  • 可以在web中
    • 使用shell命令
  • 可以用表单
    • 来做个加法器吗?
  • 下次再说!👋

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。