设计思路:
需求中提出了在客户端用python读取A.txt并以POST形式发送到服务器端;
所以,这里需要在客户端进行文件读取,并发送post请求;
服务器端要使用web服务提供POST请求服务,然后对数据进行解析并入库。
客户端代码:
__author__ = 'toriycn'
import urllib
import urllib.request
import requests
file = open("/Users/toriycn/Downloads/A.txt")
while 1:
line = file.readline()
if not line:
break
data = {
#"arg" : "Time: 2017-07-19 11:35:59, Thread ID:[146], Level: INFO , Type: XinShengJinYe.UI.Common.Helpers.RegisterHelper, message: 客户8516678为推广商或激活用户无需标记客户黑名单类型"
"arg" : line
}
print(data)
r = requests.post("http://127.0.0.1:5000",data=data)
print(r.text)
服务器端代码:
__author__ = 'toriycn'
from flask.globals import request
import re
import urllib
from flask import Flask
app = Flask(__name__) #创建Flask application对象
app.debug = True
@app.route('/',methods=['GET', 'POST'])
def index():
print(request.form)
#post参数获取
parampostlog = request.form.get("arg", "")
print(parampostlog,"ddddd")
#解析log转换为map
d = dict([s.strip().split(':',1) for s in parampostlog.split(',')])
print(d)
insertsql = "insert into tab(Time,Thread ID,Level,Type,message) values('%(Time)s','%(Thread ID)s','%(Level)s','%(Type)s','%(message)s')"%d
print(insertsql)
return "OK"
app.run(host='0.0.0.0')
然后将数据入库即可;
有什么需要向我请教的问题:欢迎加入Python高级编程,群号码:131894760;