-
Notifications
You must be signed in to change notification settings - Fork 2
/
请求捕获数据库生成.py
102 lines (79 loc) · 3.23 KB
/
请求捕获数据库生成.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import flask
from flask import Flask,jsonify,request,render_template,redirect
from flask import session,url_for,escape,Response
import sqlalchemy
import flask_cors
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import sessionmaker
# engine = create_engine('mysql+pymysql://root:[email protected]/test?charset=utf8')
class MakeSqlServer(object):
def __init__(self,WebServer):
self.web = WebServer
self._sql_init(db='mysql')
def _sql_init(self,**kw):
_config = {'mysql':self._mysql_init,'sqlite':self._sqlite_init}
if 'db' in kw and kw['db'] in _config:
_config[kw['db']]()
def _mysql_init(self,**kw):
_config = {'Host':'','User':'root','Pwd':'root','Db':'test','Charset':'utf8'}
_config.update(kw)
engine = create_engine('mysql+pymysql://{User}:{Pwd}@{Host}/{Db}?charset={Charset}'.format(**_config),echo=True)
self.conn = engine.connect()
self.metadata = MetaData(engine)
def _sqlite_init(self,**kw):
_config = {'Host':''}
_config.update(kw)
engine = create_engine('sqlite:///:memory:')#,echo=True
self.conn = engine.connect()
self.metadata = MetaData()
def createTable(self,tableName,*args,**kw):
#_config = {"tablename":tableName,}
ls = [Column('id',Integer,primary_key=True,autoincrement = True)]
for i in args:
# if i in kw:
# ls.append(Column(i,String(kw[i])))
# else:
# ls.append(Column(i,String(100)))
ls.append(Column(i,String(100)))
table_ls = Table(tableName,self.metadata,*ls)
return table_ls
def handle_reqeust(self):
req_data = request.json or request.form or request.data
table = self.createTable(request.path[1:],*list(req_data.keys()),**request.args)
print(table.name)
print(request.args)
try:
table.create()
return 'true'
except sqlalchemy.exc.InternalError as e:
print(e)
self.conn.execute('drop table {tablename}'.format(tablename=table.name))
table.create()
return str(e)
except InvalidRequestError as e:
print(e)
self.conn.execute('drop table {tablename}'.format(tablename=table.name))
table.create()
return (str(e))
class WebServer(object):
def __init__(self,**kw):
self._config = {'host':'0.0.0.0','port':8080}
self.app = Flask(__name__)
def append_route(self,routes,**kw):
for route_item in routes:
self.app.add_url_rule(route_item['path'],route_item['func'].__name__,route_item['func'],methods= route_item['method'] if 'method' in route_item else ['GET'])
def runing(self,*args,**kw):
self._config.update(kw)
self.app.run(**self._config)
if __name__ == '__main__':
server = WebServer()
Handle = MakeSqlServer(server)
route_rule = [{
'path':'/','func':lambda:'hello'
},{
'path':'/authcheck','func':Handle.handle_reqeust,'method':['GET','POST']
}]
server.append_route(route_rule)
server.runing()