python包,允许创建用于与套接字通信的简单服务器和客户端

sockets的Python项目详细描述


关于

Python package which allows creation of simple servers and clients for communication with sockets. Supports both Python2 and Python3.
Version: 1.0.0

用法

- To create a server: Depending on your Python version, import the Server class from the appropriate server module, subclass it and override its act_on() method which describes what it should do when it receives a request, and returns a string response. Finally, create a Server object and call its listen() method.

- To create a client: Depending on your Python version, import the Client class from the appropriate client module, create a Client object and call its poll_server() method. You can then make use of the response it returns as required.

By default, a Python2 client will poll the default Python3 server while a Python3 client will poll the default Python2 server (unlike in the examples below).

示例

# Test server with Python2:
from sockets.python2.server import Server
class MyServer(Server):
    def act_on(self, data, addr):
        # Do something with data (in bytes) and return a string.
        return data
server = MyServer(listening_address=('127.0.0.1', 11112))
server.listen()

# Test client with Python2. Polls the Python2 server.
from sockets.python2.client import Client
client = Client()
response, addr = client.poll_server("Hello world", server=('127.0.0.1', 11112))
print response, addr

# Test server with Python3:
from sockets.python3.server import Server
class MyServer(Server):
    def act_on(self, data, addr):
        # Do something with data (in bytes) and return a string.
        return data.decode()
server = MyServer(listening_address=('127.0.0.1', 11113))
server.listen()

# Test client with Python3. Polls the Python3 server.
from sockets.python3.client import Client
client = Client()
response, addr = client.poll_server("Hello world", server=('127.0.0.1', 11113))
print(response, addr)

撰稿人

Ehiorobo Evans (2018).

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java OpenShift的齿轮特性   java如何在Liferay站点的每个页面上放置公司地址和电话?   java确定整数数组中是否存在一个子集,在两个条件下求和到给定的目标值   序列化为什么java中的serialVersionUID必须是静态的、最终的、长类型的?   java响应返回null   java注入接口实现Quarkus   java我不明白为什么第二次排序的运行时间比第一次慢?   (Java)显示图像的最佳方式?   java Android应用程序因添加布局而崩溃   java如何在运行时获取泛型变量的类   java Selenium web驱动程序:无效的选择器:*:WebKitFullScreenSentor   Spring中的java注入值始终为空   Eclipse中带有TestNG插件的@BeforeSuite和@AfterSuite的java问题   使用trycatch块、filewriter和printwriter在java中创建自定义类   如何在Java 安卓上绘制相交的两条线