资源简介

   这是一个基于Python的Socket服务器,使用monkey,同时支持websocket、http和原生tcp三种请求。

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
# @Time    : 2018/1/24 17:22
# @Author  : Py.qi
# @File    : gevent_sock.py
# @Software: PyCharm
import socketgeventdatetimebase64hashlibre
from gevent import monkey
from dbDrive import Common
from dbDrive import server_cfg
import multiprocessingtimeuuiddatetimejson
from multiprocessing import Process Queue
monkey.patch_all()
class NewClient:
    handle = 0
    type = 0  # socket类型0为普通TCP socket 1为websocket  2为UDP  3为post
    num = 0  # 接收消息次数
    host = ““
    port = 0
    def __init__(self sock type=0):
        self.type = type
        self.handle = sock
        address=sock.getpeername()
        self.host = address[0]
        self.port = address[1]
class SocketServer(object):
    logOutQueue=0
    RecvIn=0
    ServerPort=0
    TCPServerHandle=0
    maxsize=10240
    zipEnable=True
    UDPSocketHandle=0
    ClientInfos={}
    def __init__(self):
        self.ServerPort=0
        pass
    def initTCPServer(self):
        self.TCPServerHandle = socket.socket(socket.AF_INET socket.SOCK_STREAM)
        self.TCPServerHandle.setsockopt(socket.SOL_SOCKET socket.SO_REUSEADDR 1)
        self.TCPServerHandle.bind((‘‘ self.ServerPort))
        self.TCPServerHandle.listen(10)
        _ self.ServerPort = self.TCPServerHandle.getsockname()
        #self.logouts(“日志消息““TCPServer开启成功,端口号“self.ServerPort)
    def initUDPServer(self):
        self.UDPSocketHandle = socket.socket(socket.AF_INET socket.SOCK_DGRAM)  # protocol
        self.UDPSocketHandle.setsockopt(socket.SOL_SOCKET socket.SO_REUSEADDR 1)
        self.UDPSocketHandle.bind((‘‘ self.ServerPort))
        __ self.ServerPort = self.UDPSocketHandle.getsockname()
        self.logouts(“日志消息“ “Server000开启成功,端口号“ self.ServerPort)
    def logouts(self kind=“日志消息“ *params exp=8):
        ‘‘‘
            输出日志
        ‘‘‘
        o = str(datetime.datetime.now())
        s = ““
        for i in params:
            s += “ “ + str(i)
        logstr = “ “ + o + “:“ +kind+“  “+ “ “ + s
        print(logstr)
        try:
            self.logOutQueue.put(
                Common.zipStr(json.dumps({“type“: “log“ “data“: {“日志时间“: o “日志内容“: s “日志类型“: kind}})).decode(
                    “utf8“ “ignore“))

        except Exception as error:
            pass
        try:
            logdata= {
                    “addDatas“: {
                        server_cfg.LogServer_log: [
                            {
                                “data“: {
                                    “CName“: self.name
                                    “LogContent“: “ “ + o + “:“ + self.name + “ “ + s
                                    “LogKind“: kind
                                    “LogCreateTime“: o
                                }
                            }
                        ]
                    }
                }

            self.SendDataToDBServer(“-2“logdata)
        excep

评论

共有 条评论