zoukankan      html  css  js  c++  java
  • 第25章 项目6:使用CGI进行远程编辑

    初次实现

    25-1 simple_edit.cgi ——简单的网页编辑器

    #!D:Program Filespython27python.exe
    import cgi
    form = cgi.FieldStorage()

    text = form.getvalue('text', open('simple_edit.dat').read())
    f = open('simple_edit.dat', 'w')
    f.write(text)
    f.close()
    print """Content-type: text/html

    <html>
    <head>
    <title>A simple Editor</title>
    </head>
    <body>
    <form action='simple_edit.cgi' method='POST'>
    <textarea rows='10' cols='20' name='text'>%s</textarea><br />
    <input type='submit' />
    </form>
    </body>
    </html>
    """ % text

    simple_edit.dat文件:随意输入一些即可。

    将此脚本及空白的simple_edit.dat文件(没有该文件则无法运行)放在D:Program FilesApache24cgi-bin目录下

    在浏览器中输入:http://localhost/cgi-bin/simple_edit.cgi

    输出如下:

    按照书上输入Nickety, nockety, noo, noo, noo...并提交

    从页面看没什么变化,但simple_edit.dat中写入了以上内容

    再次打开http://localhost/cgi-bin/simple_edit.cgi,页面显示如下:

    且每次在查询内容中输入不同的字符,都会重新写入simple_edit.dat

     

    再次实现

    CGI脚本拆分为3个:一个带有能输入文件名的表单的网页index.html,在文本域中显示给定文件的脚本edit.cgi,保存收到的文本到给定文件的脚本save.cgi

    index.htmlHTML文件,包括用于输入文件名的表单

    <html>
    <head>
    <title>File Editor</title>
    </head>
    <body>
    <form action='edit.cgi' method='POST'>
    <b>File name:</b><br />
    <input type='text' name='filename' />
    <input type='submit' value='Open' />
    </body>
    </html>

    25-2 edit.cgi ——编辑器脚本

    edit.cgi显示网页,还有一个用于输入密码的文本框。

    abspath函数被用于获取data目录的绝对路径,文件名保存在hidden表单元素中。会被传递到下一个脚本save.cgi中。

    #!D:Program Filespython27python.exe

    print 'Content-type: text/html '

    from os.path import join, abspath

    import cgi, sys

    BASE_DIR = abspath('data')

    form = cgi.FieldStorage()
    filename = form.getvalue('filename')
    if not filename:
    print 'Please enter a file name'
    sys.exit()
    text = open(join(BASE_DIR, filename)).read()

    print """
    <html>
    <head>
    <title>Editing...</title>
    </head>
    <body>
    <form action='save.cgi' method='POST'>
    <b>File:</b> %s<br />
    <input type='hidden' value='%s' name='filename' />
    <b>Password:</b><br />
    <input name='password' type='password' /><br />
    <b>Text:</b><br />
    <textarea name='text' cols='40' rows='20'>%s</textarea><br />
    <input type='submit' value='Save' />
    </form>
    </body>
    </html>
    """ % (filename, filename, text)

    密码处理——sha模块(SecureHash Algorithm,安全哈希算法)

    从输入字符串中提取看似随机数据的根本上无意义字符串的一种方法。

    >>> from sha import sha

    >>> sha('foobar').hexdigest()

    '8843d7f92416211de9ebb963ff4ce28125932878'

    >>> sha('foobaz').hexdigest()

    '21eb6533733a5e4763acacd1d45a60c2e0e404e1'

    密码中的微小改变会输出完全不同的摘要。

     

    25-3 save.cgi ——实现保存功能的脚本

    处理前端提交的数据,并保存:接受一个文件名、一个密码和一些文本,并且检查密码是否正确。

    #!D:Program Filespython27python.exe

    print'Content-type: text/htmln'

    from os.path import join, abspath
    import cgi, sha, sys

    BASE_DIR = abspath('data')

    form = cgi.FieldStorage()
    text = form.getvalue('text')
    filename = form.getvalue('filename')
    password = form.getvalue('password')

    if not(filename and text and password):
    print 'Invalid parameters.'
    sys.exit()
    if sha.sha(password).hexdigest() != '8843d7f92416211de9ebb963ff4ce28125932878':
    print 'Invalid password'
    sys.exit()

    f = open(join(BASE_DIR, filename), 'w')
    f.write(text)
    f.close()

    print 'The file has been saved.'

    index.html及脚本edit.cgisave.cgi放入到D:Program FilesApache24htdocs目录下,并创建data文件夹,新建空白的edit.dat文件。

    在浏览器中输入:localhost127.0.0.1即可调出 index.html

    输入edit.dat然后点击open

    密码默认为foobar,输入文本Nickety, nockety, noo, noo, noo...,并提交:

     

    文本写入到edit.dat中:

    若密码输入错误,显示如下:

    若无任何密码或文本输入,显示如下:

  • 相关阅读:
    环境是如何建立的 启动文件有什么
    环境中存储的是什么
    串行 并行 异步 同步
    TPC-H is a Decision Support Benchmark
    进程通信类型 管道是Linux支持的最初Unix IPC形式之一 命名管道 匿名管道
    删除环境变量
    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
    14.3.2.2 autocommit, Commit, and Rollback 自动提交 提交和回滚
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
  • 原文地址:https://www.cnblogs.com/Sumomo0516/p/6131711.html
Copyright © 2011-2022 走看看