1 Star 0 Fork 14

kk3950 / HPSocket4Python

forked from RonxBulld / HPSocket4Python
暂停
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

HPSocket4Python

这个是 HPSocket 的 Python 绑定,力图在 Python 上更方便的使用 HPSocket 组件。目前已经可以通过继承类的方式来使用 Tcp_Pull_Server。 代码形如:

# coding: utf-8

import time,sys,os
sys.path.append(os.getcwd())
sys.path.append(os.getcwd()+'\\..\\')

from HPSocket import TcpPull
from HPSocket import helper
import HPSocket.pyhpsocket as HPSocket

class Server(TcpPull.HP_TcpPullServer):
    def Start(self, host, port):
        if super().Start(host,port):
            print('Start server success, listen on %s:%d'%(host,port))
            return True
        else:
            print('Start server fail.')
            return False

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnPrepareListen(self, Sender, SocketHandler):
        return HPSocket.EnHandleResult.HR_OK

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnSend(self, Sender, ConnID, Data):
        print('[TRACR] [Server] send to %d -> [length=%d]%s'%(ConnID, len(Data), repr(Data)))
        return HPSocket.EnHandleResult.HR_OK

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnClose(self, Sender, ConnID, Operation, ErrorCode):
        print('The custom out: %d[Opt=%d, Err=%d]'%(ConnID, Operation, ErrorCode))
        return HPSocket.EnHandleResult.HR_OK

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnAccept(self, Sender, ConnID, Client):
        self.pkg = helper.TPkgInfo(True, helper.TPkgHeaderSize)     # 这块内存需要保护起来
        HPSocket.HP_Server_SetConnectionExtra(Sender, ConnID, self.pkg)
        print('New custom in: %d'%ConnID)
        return HPSocket.EnHandleResult.HR_OK

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnReceiveHead(self, Sender, ConnID, Seq:int, Length:int):
        print('[TRACR] [Server] head -> seq: %d, body_len: %d' % (Seq, Length))

    @TcpPull.HP_TcpPullServer.EventDescription
    def OnReceiveBody(self, Sender, ConnID, Body: bytes):
        (name, age, desc) = helper.GeneratePkg(Body)
        print('[TRACE] body -> name: %s, age: %d, desc: %s' % (name,age,desc))
        Buf = helper.GeneratePkgBuffer(seq=-1,name=name,age=age,desc=desc)
        self.Send(Sender, ConnID, Buf)


svr = Server()
if svr.Start('0.0.0.0',5555):
    while True:
        time.sleep(1)
The MIT License (MIT) Copyright (c) 2017 RonxBulld Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

这个是HPSocket的Python绑定 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/kk3950/HPSocket4Python.git
git@gitee.com:kk3950/HPSocket4Python.git
kk3950
HPSocket4Python
HPSocket4Python
master

搜索帮助