基于tcp socketserver的简单客户机-服务器库
iSock的Python项目详细描述
基于tcp socketserver的简单客户机-服务器库
ISocket是一个库,它允许在Python中快速开发客户机-服务器应用程序。 如果您开发的软件运行在多台相互通信的机器上,这个库可能会派上用场。
主要功能:
- 非常简单的服务器配置。
- 轻松地来回传输复杂的数据结构(所有可拾取的数据都可以发送和接收)。
- ISocket结构允许对应用程序进行干净的设计。
- 客户端可以方便地访问服务器上的变量。
Isocket是用Python2.7编写的,在Windows和Linux上工作。源代码可以在这里找到:https://github.com/0x1001/isock
ISocket示例
下面的示例代码显示了使用示例。 您可以在这里学习如何启动服务器和客户端以及如何定义服务器操作。
本例中使用的操作列表:
- 回显-将所有数据发送回客户端。
- exec-在服务器上执行系统调用并将控制台输出传输到客户端。
- exechistory-向客户端发送系统调用历史记录。
- 时间-将当前服务器时间发送到客户端。
示例:
import threading from isock import Server from isock import Client from isock import Action ################################################################################ ############################ Server actions #################################### ################################################################################ class Echo(Action): def action(self,data): return data class Exec(Action): def __init__(self,exec_history): self.exec_history = exec_history def action(self,data): import subprocess self.exec_history.append(data) return subprocess.check_output(data,shell=True) class ExecHistory(Action): def __init__(self,exec_history): self.exec_history = exec_history def action(self,data): return self.exec_history class Time(Action): def action(self,data): import datetime return datetime.datetime.now() ################################################################################ ############################ Server startup #################################### ################################################################################ history = [] server = Server("localhost",4440) server.addAction(Echo()) server.addAction(Exec(history)) server.addAction(ExecHistory(history)) server.addAction(Time()) server_thread = threading.Thread(target=server.serve_forever) server_thread.start() ################################################################################ ############################ Client session #################################### ################################################################################ client = Client("localhost",4440) print "############################# Echo test ################################" print client.runAction(Echo,"Echo test!") print "############################# Exec test ################################" print client.runAction(Exec,"dir") print client.runAction(Exec,["python","-V"]) print "############################# Exec history #############################" print client.runAction(ExecHistory) print "############################# Server time ##############################" print client.runAction(Time) ################################################################################ ############################ Server shutdown ################################### ################################################################################ server.shutdown() server.server_close() server_thread.join()
ISocket服务器指南
服务器可以从isock导入:
from isock import Server
服务器构造函数接受两个必需的参数:ip(string)和port(int):
server = Server("localhost",4440)
要向服务器添加操作,请使用addAction方法。它需要一个必需的参数:action(action):
server.addAction(Echo())
要启动服务器,请使用serve_forever()阻塞方法:
server.serve_forever()
要停止服务器,请使用shutdown()方法:
server.shutdown()
ISocket客户指南
客户端可以从ISOCK导入:
from isock import Client
客户端构造函数接受两个必需参数和一个可选参数:server_ip(string)、server_port(int)、retry(int)。 默认重试参数设置为3。它允许在引发clientException之前重试3次:
client = Client("localhost",4440)
要在服务器上运行操作,请使用run action()方法,该方法接受一个必需参数和一个可选参数:action(action class ref)、data(any pickable data):
client.runAction(Echo,"Echo test!")
客户端方法runAction()返回服务器发送的数据,如果操作在服务器上以异常结束,则返回rasies异常。
ISocket操作指南
服务器可以执行的所有操作都必须从Action类继承。 动作类可以从isock导入:
from isock import Action
客户端和服务器都必须有权访问用户定义的操作类。我建议将它们保存在单独的文件中,可以在服务器和客户机中导入。 若要定义新操作,请创建继承自操作类并重写action()方法的新类。此方法采用一个必需的参数:data(客户端发送的任何可拾取数据)。 如果客户端不发送任何数据,则此参数设置为“无”:
class Echo(Action): def action(self,data): return data
方法action()返回发送回客户端的数据。
要访问action类中的服务器变量,请定义构造函数,该构造函数将对服务器变量的引用存储为属性,然后可以在action方法中访问该属性:
class ExecHistory(Action): def __init__(self,exec_history): self.exec_history = exec_history def action(self,data): return self.exec_history
贡献
欢迎大家参与这个项目。github上提供了源代码。 https://github.com/0x1001/isock