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第三方库


热门话题
excel如何检查java 6中日期类型的双精度值   java访问Apache Tapestry页面中上载的文件   java ApiException required Throwable,found CustomException   java简单添加应用程序“很遗憾,…”无法工作   java字符串数组:“从未使用在“i++”处更改的值”   java如何使用泛型来提供“通用getter方法”?   java EJB 2.1性能问题和验证根本原因   java网络爬虫对服务器的连续请求之间的最小延迟   java如何将节点作为参数传递   java Dropwizard:如何在使用dhatim/dropwizardsentry时设置Sentry用户上下文[Fixed]   用JAVA中给定的首字母将该行添加到数组中   java如何扫描字符串中以逗号分隔的元素?   java MultiFormatReader无法读取qr,尽管qr是可扫描的   java为什么不能在接口类型上引用类的方法   java是一种从开始到结束查找路径的有效方法   java访问嵌套LinkedList中的项的子项