Skip to content

Latest commit

 

History

History
86 lines (57 loc) · 1.46 KB

README.md

File metadata and controls

86 lines (57 loc) · 1.46 KB

PyKCP

KCP protocol by python

Usage

1.Create KCP object

from pykcp.kcp import KCP

conv = 12345678

def output(kcp, data):
    '''
    KCP lower layer protocol output function
    which will be called by KCP when it needs to send data
    '''
    pass

kcp = KCP(conv, output)

2.Call update in an interval

import time

kcp.update(int(time.time() * 1000))

3.Input a lower layer data packet

kcp.input(data)

4. Receive data from KCP

data = kcp.recv()

Example

There is a simple TCP server and a simple TCP client in pykcp. It base from tornado framework.

For example a echo service like this

echo server

from pykcp.tcpserver import TCPServer
from tornado.ioloop import IOLoop

class TestServer(TCPServer):

    def handle_message(self, kcpstream, msg):
        print('RECV: %s' % msg)
        kcpstream.send(b'>>>> %s' %msg)

if __name__ == '__main__':
    server = TestServer()
    server.listen(8888)

echo server

from pykcp.tcpclient import TCPClient
from tornado.ioloop import IOLoop

class TestClient(TCPClient):

    def handle_connect(self):
        self.kcpstream.send(b'hello kcp')

    def handle_message(self, kcpstream, msg):
        print('RECV: %s' % msg)
        kcpstream.send(b'++++ %s' %msg)

if __name__ == '__main__':
    client = TestClient()
    client.kcp_connect('127.0.0.1', 8888)