Python使用webpy制作简单的WEB服务器,支持数据库的增删改查,文件上传等功能
import web,json,sys
urls=(
'(/|/index.html)','index',
'/wyw/dbQuery','dbQuery',
'/wyw/upload', 'upload',
'/(.*?)','normal'
)
#获取第二个参数为数据库名称(第一个参数为服务器端口号)
dbname=sys.argv[2]
if dbname=="": dbname="guangdiu"
db=web.database(dbn='mysql',host="localhost",port=3306,db=dbname,user='root',pw="admin")
#上传文件
class upload:
def POST(self):
x = web.input(myfile={})
f=open('static/upload/'+x['myfile'].filename,'wb')
f.write(x['myfile'].value)
return json.dumps({"state":"OK"})
#一般文件获取
class normal:
def GET(self,arg1):
f=open('static/'+arg1,'rb')
return f.read()
#首页获取
class index:
def GET(self,arg1):
f=o