(45)-- 用Python 简单访问 MongoDB
2018-03-15 21:03
411 查看
# 用Python 简单访问 MongoDB
# server.pyfrom http.server import HTTPServer, CGIHTTPRequestHandler port = 8000 httpd = HTTPServer(('', port), CGIHTTPRequestHandler) print("Starting simple_httpd on port: " + str(httpd.server_port)) httpd.serve_forever()
# index.html<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<a href="add.html">用户添加</a>
</body>
</html>
# add.html
# add.py
#!/usr/bin/python3
import cgi,cgitb,pymongo
cgitb.enable()
print("Content-Type: text/html;charset=utf-8") # HTML is following
print() # blank line, end of headers
# 接收数据
fs = cgi.FieldStorage()
data = {}
for key in fs.keys():
data[key] = fs[key].value
# print(data)
#链接到 127.0.0.1 的mongo
client = pymongo.MongoClient('127.0.0.1', 27017)
#选择test库
db = client.py7
# print(db)
# 执行添加
db.user.insert(data)
print('<script>location.href="/cgi-bin/index.py"</script>')
#!/usr/bin/python3
import cgi,cgitb,pymongo
cgitb.enable()
print("Content-Type: text/html;charset=utf-8") # HTML is following
print() # blank line, end of headers
# print(data)
#链接到 127.0.0.1 的mongo
client = pymongo.MongoClient('127.0.0.1', 27017)
#选择test库
db = client.py7
# print(db)
trs = ''
# 查询出所有的用户的key
res = db.user.find()
for x in res:
# 循环读取用户信息
if x['sex'] == '0':
sex = '女'
else:
sex = '男'
print(x['username'])
# print(sex)
trs += '''
<tr>
<td>{uname}</td>
<td>{upass}</td>
<td>{uemail}</td>
<td>{usex}</td>
<td>
<a href="/cgi-bin/del.py?uid={uid}">删除</a>
<a href="/cgi-bin/edit.py?uid={uid}">编辑</a>
</td>
</tr>
'''.format(uid=x['_id'],uname=x['username'],upass=x['password'],uemail=x['email'],usex=sex)
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户列表</title>
</head>
<body>
<a href="/add.html">用户添加</a>
<center>
<table>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>性别</th>
<th>操作</th>
</tr>
{trs}
</table>
</center>
</body>
</html>
'''.format(trs=trs)
print(html)
Python学习交流、资源共享群:563626388 QQ
# server.pyfrom http.server import HTTPServer, CGIHTTPRequestHandler port = 8000 httpd = HTTPServer(('', port), CGIHTTPRequestHandler) print("Starting simple_httpd on port: " + str(httpd.server_port)) httpd.serve_forever()
# index.html<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<a href="add.html">用户添加</a>
</body>
</html>
# add.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>添加页面</title> </head> <body> <form action="/cgi-bin/add.py" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 邮箱:<input type="text" name="email"><br> 性别:<input type="radio" name="sex" value="0" checked>女 <input type="radio" name="sex" value="1">男<br> <br> <button>添加</button> </form> </body> </html>
# add.py
#!/usr/bin/python3
import cgi,cgitb,pymongo
cgitb.enable()
print("Content-Type: text/html;charset=utf-8") # HTML is following
print() # blank line, end of headers
# 接收数据
fs = cgi.FieldStorage()
data = {}
for key in fs.keys():
data[key] = fs[key].value
# print(data)
#链接到 127.0.0.1 的mongo
client = pymongo.MongoClient('127.0.0.1', 27017)
#选择test库
db = client.py7
# print(db)
# 执行添加
db.user.insert(data)
print('<script>location.href="/cgi-bin/index.py"</script>')
#!/usr/bin/python3
import cgi,cgitb,pymongo
cgitb.enable()
print("Content-Type: text/html;charset=utf-8") # HTML is following
print() # blank line, end of headers
# print(data)
#链接到 127.0.0.1 的mongo
client = pymongo.MongoClient('127.0.0.1', 27017)
#选择test库
db = client.py7
# print(db)
trs = ''
# 查询出所有的用户的key
res = db.user.find()
for x in res:
# 循环读取用户信息
if x['sex'] == '0':
sex = '女'
else:
sex = '男'
print(x['username'])
# print(sex)
trs += '''
<tr>
<td>{uname}</td>
<td>{upass}</td>
<td>{uemail}</td>
<td>{usex}</td>
<td>
<a href="/cgi-bin/del.py?uid={uid}">删除</a>
<a href="/cgi-bin/edit.py?uid={uid}">编辑</a>
</td>
</tr>
'''.format(uid=x['_id'],uname=x['username'],upass=x['password'],uemail=x['email'],usex=sex)
html = '''
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户列表</title>
</head>
<body>
<a href="/add.html">用户添加</a>
<center>
<table>
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>性别</th>
<th>操作</th>
</tr>
{trs}
</table>
</center>
</body>
</html>
'''.format(trs=trs)
print(html)
from http.server import HTTPServer, CGIHTTPRequestHandler port = 8000 httpd = HTTPServer(('', port), CGIHTTPRequestHandler) print("Starting simple_httpd on port: " + str(httpd.server_port)) httpd.serve_forever()兄弟连学python
Python学习交流、资源共享群:563626388 QQ
相关文章推荐
- 菜鸟学python(7) 简单文件访问
- 一个简单的使用代理访问百度页面内容的python脚本
- python简单实现爬取小说《天龙八部》,并在页面本地访问
- python编写简单程序访问A8系统
- 利用python2.7自带的简单的web服务器SimpleHTTPServer实现web页面的访问
- Python 操作mongodb 简单实例
- python 下 mongodb 简单操作整理
- python操作mongodb 简单Demo-mongodb -version 3.4.0
- Python+MongoDB自增键值的简单实现
- Python对Mongodb的一些简单操作
- 用python简单处理图片(4):图像中的像素访问
- 用python简单处理图片(4):图像中的像素访问
- python直接访问私有属性的简单方法
- python简单实现爬取小说《天龙八部》,并在页面本地访问
- MongoDB最简单的入门教程之五-通过Restful API访问MongoDB
- 从Python程序中访问Java类的简单示例
- 用python简单处理图片(4):图像中的像素访问
- python直接访问私有属性的简单方法
- Python 简单的并发访问http
- python 连接 mongodb 简单例子