问题反馈小平台实现 ----python练习

python版本python3.6.1

import tornado.ioloop
import tornado.web
import datetime
#!gbk

class MainHandler(tornado.web.RequestHandler):
def get(self):
print('get')
u = self.get_argument('user')
p = self.get_argument('pwd')
if u == 'admin' and p == '123':
self.write('登陆成功!')
else:self.write('登陆失败!')

def post(self, *args, **kwargs):

u = self.get_argument('user')
pr = self.get_argument('depa')
p = self.get_argument('problem')
fh = open('hgr', 'a+')
fh.write(datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')+' ')
fh.write(u+' ')
fh.write(pr+' ')
fh.write(p+' ')
fh.close()
print(datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S'),u,pr,p)
self.write('提交成功!请等待稍后处理问题。(^_^)')

application = tornado.web.Application([
(r"/index", MainHandler),
])

if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

运行上面的代码 后 把以下html代码 放httpd /var/www/html/中


index.html


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" method="GET">
<title>Title</title>
</head>
<body>
<form action="http://192.168.1.62:8888/index" method="POST">
姓名:<input type="text" name="user"/>
部门:<input type="text" name="depa"/>
问题描述:<input type="text" name="problem"/>
<input type="submit" value="提交" />
</form>
</body>
</html>
注:192.168.1.62:8888 python运行的IP和端口

运行httpd服务

可以 看到程序打印出日志,且文件中也有写入数据。




原文地址:https://www.cnblogs.com/hmysql/p/9134874.html