Python通过TCP连接读取json对象(使用regex?)

2024-07-03 04:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

通过我的客户端发送json对象。每个对象的形式如下:

{
       "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"
    ]
}

我想写一个python服务器,它发送到端口11111,并且能够获得这样的对象并分别解析它们。在

有人能帮我完成一个完整的代码吗?在

非常感谢!在


Tags: 对象inipsrceventtypealerttimestamp
2条回答

你可以用烧瓶。请随意挖掘文档:http://flask-restful-cn.readthedocs.io/en/0.3.4/

尤其是完整的示例为您提供了足够的信息来实现您的目标:(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)

您可以使用SocketServer包。文档中给出了一些可能对您有用的小示例。以下是tcp服务器的扩展示例:

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()

相关问题 更多 >