設計思路:python
需求中提出了在客戶端用python讀取A.txt並以POST形式發送到服務器端;web
因此,這裏須要在客戶端進行文件讀取,併發送post請求;sql
服務器端要使用web服務提供POST請求服務,而後對數據進行解析併入庫。編程
客戶端代碼:flask
__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;app