2016-07-26 8 views
-1

Mein Client sendet JSON-Objekte über TCP. Jedes Objekt ist mit der folgenden Form:Python - json Objekt über TCP-Verbindung (mit Regex?) Lesen

{ 
     "message" => "{\"timestamp\":\"2016-07-21T01:20:04.392799-0400\",\"in_iface\":\"docker0\",\"event_type\":\"alert\",\"src_ip\":\"172.17.0.2\",\"dest_ip\":\"172.17.0.3\",\"proto\":\"ICMP\",\"icmp_type\":0,\"icmp_code\":0,\"alert\":{\"action\":\"allowed\",\"gid\":2,\"signature_id\":2,\"rev\":0,\"signature\":\"ICMP msg\",\"category\":\"\",\"severity\":3},\"payload\":\"hFuQVwAA\",\"payload_printable\":\"kk\"}", 
     "@version" => "1", 
    "@timestamp" => "2016-07-25T04:41:11.980Z", 
      "path" => "/etc/logstash/jsonSample.log", 
      "host" => "baklava", 
      "doc" => { 
       "timestamp" => "2016-07-21T01:20:04.392799-0400", 
       "in_iface" => "docker0", 
       "event_type" => "alert", 
        "src_ip" => "172.17.0.2", 
        "dest_ip" => "172.17.0.3", 
        "proto" => "ICMP", 
       "icmp_type" => 0, 
       "icmp_code" => 0, 
        "alert" => { 
        "action" => "allowed", 
        "gid" => 2, 
      "signature_id" => 2, 
        "rev" => 0, 
       "signature" => "ICMP msg", 
       "category" => "", 
       "severity" => 3 
     }, 
        "payload" => "hFuQVwAA", 
     "payload_printable" => "kk" 
    }, 
    "alert.gid" => 2, 
      "tags" => [ 
     [0] "tagName_2" 
    ] 
} 

Ich möchte ein Python-Server schreiben, die auf Port 11111 listent, und ist in der Lage solche Objekte zu erhalten und analysieren sie separat.

Kann jemand mit einem vollständigen Code helfen?

Vielen Dank!

+0

Bitte beachten Sie, dass diese Plattform keine Codierung Service ist mit Batterien enthalten. Versuchen Sie es selbst, zeigen Sie uns Ihren Code, holen Sie Hilfe und versuchen Sie es erneut. Auf diese Weise werden Sie lernen, wie Sie fortfahren. – ferdy

Antwort

0

Sie können das SocketServer Paket verwenden. Die Dokumentation enthält kleine Beispiele, die für Sie nützlich sein können. Hier ist ein erweitertes Beispiel für einen TCP-Server:

import SocketServer 
import os 
import logging 
import json 

FORMAT = '[%(asctime)-15s] %(message)s' 
logging.basicConfig(format=FORMAT, level=logging.DEBUG) 


class MyServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): 
    # By setting this we allow the server to re-bind to the address by 
    # setting SO_REUSEADDR, meaning you don't have to wait for 
    # timeouts when you kill the server and the sockets don't get 
    # closed down correctly. 
    allow_reuse_address = True 

    request_queue_size = 10 

    def __init__(self, port): 
     self.host = os.uname()[1] 
     self.port = port 

     SocketServer.TCPServer.__init__(self, (self.host,self.port), MyTCPHandler) 

     logging.info("Server has been started on {h}:{p}".format(h=self.host,p=self.port)) 


class MyTCPHandler(SocketServer.BaseRequestHandler): 
    """ 
    The RequestHandler class for our server. 

    It is instantiated once per connection to the server, and must 
    override the handle() method to implement communication to the 
    client. 
    """ 

    def handle(self): 
     # self.request is the TCP socket connected to the client 
     # max length is here 2048 chars 
     s = self.request.recv(2048).strip() 

     logging.info("received: {d}".format(d=s)) 

     # here you may parse the received string 
     obj = json.loads(s) 

     # here just send something back to server 
     self.request.sendall("got it") 


PORT = 11111 

if __name__ == "__main__": 
    # Create the server, binding to localhost on port 11111 
    #server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) 
    server = MyServer(PORT) 

    # Activate the server; this will keep running until you 
    # interrupt the program with Ctrl-C 
    server.serve_forever() 
0

Sie können FlascheRESTful verwenden. Fühlen Sie sich frei in der Dokumentation zu graben: http://flask-restful-cn.readthedocs.io/en/0.3.4/

Vor allem die vollständige Beispiel Sie genügend Informationen gibt Ihr Ziel zu erreichen: (http://flask-restful-cn.readthedocs.io/en/0.3.4/quickstart.html#full-example)

from flask import Flask 
from flask_restful import reqparse, abort, Api, Resource 

app = Flask(__name__) 
api = Api(app) 

TODOS = { 
    'todo1': {'task': 'build an API'}, 
    'todo2': {'task': '?????'}, 
    'todo3': {'task': 'profit!'}, 
} 


def abort_if_todo_doesnt_exist(todo_id): 
    if todo_id not in TODOS: 
     abort(404, message="Todo {} doesn't exist".format(todo_id)) 

parser = reqparse.RequestParser() 
parser.add_argument('task') 


# Todo 
# shows a single todo item and lets you delete a todo item 
class Todo(Resource): 
    def get(self, todo_id): 
     abort_if_todo_doesnt_exist(todo_id) 
     return TODOS[todo_id] 

    def delete(self, todo_id): 
     abort_if_todo_doesnt_exist(todo_id) 
     del TODOS[todo_id] 
     return '', 204 

    def put(self, todo_id): 
     args = parser.parse_args() 
     task = {'task': args['task']} 
     TODOS[todo_id] = task 
     return task, 201 


# TodoList 
# shows a list of all todos, and lets you POST to add new tasks 
class TodoList(Resource): 
    def get(self): 
     return TODOS 

    def post(self): 
     args = parser.parse_args() 
     todo_id = int(max(TODOS.keys()).lstrip('todo')) + 1 
     todo_id = 'todo%i' % todo_id 
     TODOS[todo_id] = {'task': args['task']} 
     return TODOS[todo_id], 201 

## 
## Actually setup the Api resource routing here 
## 
api.add_resource(TodoList, '/todos') 
api.add_resource(Todo, '/todos/<todo_id>') 


if __name__ == '__main__': 
    app.run(debug=True)