资源简介
这是一个基于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
- 上一篇:12306抢票代码(基于python2)
- 下一篇:python检测图片是否有人脸
相关资源
- Python-gzipencoding如何压缩HTTP请求发送到
- apachehttpd-2.4.27-x64-vc11.zip
- python-3.7.7-amd64-webinstall.exe
- Flask实现的TCPSocketServer服务器
- 基于Python的http流抓取系统
- python写的tcp局域网聊天程序源码+exe
- python聊天室---pyqt5+socket+Thread聊天室
- Python-跳一跳改分器通过HTTP代理拦截进
- Python-aiohttp一个用于asyncio和Python的异
- python http服务器搭建
- Django2 dwebsocket 基于web消息传递 代码
- Python 聊天室 客户端和服务端 聊天
- python Django websocket 实时消息推送
- 用Python实现语音的传输功能
- python实现类似QQ群聊
- python3 包装的httpclient,支持session
- Python简版聊天室代码
- python 3.6 socket传输视频
- 基于socket的python聊天
- 树莓派http协议onenet平台案例
- python实现类似于QQ或MSN的聊天系统
- python加django加websocket实现即时通讯
- 02.6.利用Python SOCKET多线程开发FTP软件
- Prometheus数据API导出Python脚本(export
- python多人聊天服务器 socket
- SOCKET实现DES加密的全双工聊天程序
- python聊天室
- 基于python实现的http接口自动化测试框
- Websocket客户端Websocket-Client.zip
- proxy.py 在 python 中,轻量级的HTTP代理
评论
共有 条评论