import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='lesson54')
cursor =conn.cursor(cursor=pymysql.cursors.DictCursor)
#sql="CREATE TABLE TEST(id INT ,name VARCHAR (20))"
# cursor.execute(sql)
#ret=cursor.execute("insert into test VALUES (1,'fengfeng'),(2,'xiaoming')")
#print(ret)
ret1 = cursor.execute("select * from test")
print(ret1)
# print(cursor.fetchone())
# print(cursor.fetchmany(2))
print(cursor.fetchall())
conn.commit()
cursor.close()
conn.close()
wsgiref
from wsgiref.simple_server import make_server
import time
def fool(req):
f=open("index1.html","rb")
data = f.read();
return data
def fool2(req):
f=open("index2.html","rb")
data = f.read()
return data
def login(req):
print(req)
print(req["QUERY_STRING"])
return b'welcome!'
def signup(req):
pass
def show_time(req):
times=time.ctime()
#return ("<h1>time:%s</h1>"%str(times)).encode("utf8")
f=open("show_time.html","rb")
data=f.read()
data_new1 = data.decode("utf8")
data_new2 = data_new1.replace("@time@",str(times))
return data_new2.encode("utf8")
def router():
url_patterns=[
("/login",login),
("/signup", signup),
("/fengfeng",fool),
("/xiaorong",fool2),
("/show_time",show_time),
]
return url_patterns
def application(environ, start_response):
print(environ)
print("path",environ["PATH_INFO"])
path = environ["PATH_INFO"];
# 响应头
start_response('200 OK', [('Content-Type', 'text/html')])
url_patterns= router()
func = None
for item in url_patterns:
if item[0] == path:
func=item[1]
break
if func:
return [func(environ)]
else:
return [b'error']
#响应体
# if path=="/fengfeng":
# #return [b'<h1>Hello, fengfeng!</h1>']
# return [fool()]
# elif path=="/xiaorong":
# #return [b'<h1>Hello, xiaorong!</h1>']
# return [fool2()]
# else:
# return [b"404"]
httpd = make_server('', 8080, application)
print('Serving HTTP on port 8000...')
# 开始监听HTTP请求:
httpd.serve_forever()