直接连接用adc(s)协议的python实现。
pyADC的Python项目详细描述
pyadc是adc支持的直接连接集线器上使用的adc协议的实现。该模块配有协议处理程序、ssl/tls支持和事件函数,用于处理来回发送给用户查看/交互的所有数据。
我之所以构建它,是因为还没有真正工作的python adc协议实现,最近的是python adc。该模块还没有完成,与本项目完全没有关系,该模块已经过测试,可用于adc协议的所有基本功能。
这个模块还没有的东西(计划在以后的版本中)是搜索、客户端到客户端的连接和文件下载。
感谢
非常特别的感谢Orochi/Crispy,他帮助我们理解了一些东西,没有它们,这个图书馆根本就不能工作,所以再次感谢我的朋友。
安装
首先,因为这需要adc基本上用于所有事情的tiger树散列,所以需要在linux上安装libmhash dev。我还没有在windows或mac上测试过。如果在基于ubuntu/debian的linux上,install-in终端如下:
sudo apt-get install libmhash-dev python-dev
现在,单独安装python mhash,因为它不再与pip一起工作:
wget http://labix.org/download/python-mhash/python-mhash-1.4.tar.gz tar -xzvf python-mhash-1.4.tar.gz cd python-mhash-1.4 sudo python setup.py install
完成后,使用pip安装pyadc:
sudo pip install pyadc
如果由于某种原因PIP没有安装正确的依赖项,它们如下所示:
tiger 0.3 - sudo pip install tiger enum34 1.0 - sudo pip install enum34
示例
下面是如何使用模块的示例:
#!/usr/bin/env python from pyadc.client import Clients def main(): clientlist = Clients() # Create new instance of Client clientlist.makeclient("adcs://somehub.com:5000", "username", "somepass", "No files to share", events) if __name__ == "__main__": main()
事件函数
现在有两种方法可以将事件函数与库接口,即自库开始以来的原始事件字典。如下所示:
events = { 'onconnected': onconnected, # parameter: hubaddress(string) 'onconnecting': onconnecting, # parameter: hubaddress(string) 'ondisconnected': ondisconnected, # parameter: hubaddress(string) 'oniprecvd': oniprecvd, # parameters: hubaddress(string), sessionid(string), ip(string) 'onjoin': onjoin, # parameters: hubaddress(string) and username(string) 'onprivatemessage': onprivatemessage, # parameters: hubaddress(string), user(object), message(string) 'onprivateemote': onprivateemote, # parameters: hubaddress(string), user(object), message(string) 'onpublicmessage': onpublicmessage, # parameters: hubaddress(string), user(object), message(string) 'onpublicemote': onpublicemote, # parameters: hubaddress(string), user(object), message(string) 'onpart': onpart, # parameters: hubaddress(string), username(string) 'onredirect': onredirect, # parameters: new hubaddress(string) 'onpassword': onpassword, # parameters: hubaddress(string) 'onstatusmessage': onstatusmessage, # parameters: hubaddress(string), status code(integer), status message(string) 'onbadpass': onbadpass, # parameters: hubaddress(string) 'ontopic': ontopic, # parameters: hubaddress(string), topic(string) 'onhubname': onhubname # parameters: hubaddress(string), hubname(string) } cl.events = events
注释中每个事件旁边列出的所有参数都是库发送它们的顺序。例如:
def onprivatemessage(hubaddress, user, message):
现在有一个类支持的版本,您可以直接将自己的类处理程序传递给库,而不是字典,下面是一个如何做到这一点的示例。所有函数名必须与事件示例匹配,类处理程序尚不支持自定义名称。:
class ADCHandler: def __init__(self, example): self.examplevar = example def onconnected(self, hubaddress): #do stuff here... def main(): clientlist = Clients() clientlist.makeclient("adcs://somehub.com:5000", "username", "somepass", "No files to share", ADCHandler(examplevar))
客户类(主要类)
clients类用于多集线器连接,它只是使您可以简单地通过一个函数调用创建一个客户机,而不是一组设置,它为您完成所有工作,对于那些不想将所有内容都键入的人,这仍然适用于一个连接。您将在下面找到clients类的属性:
clients.getclientbyaddress(hubaddress) # Retrieves the Client class of hubaddress, same as given to event functions clients.getaddressfromname(hubname) # Retreives the hubaddress from the given hub name sent by the hub. clients.makeclient(address, username, password, description, events=None, owner="", pid="") Creates and returns new Client object, also issues connect().
客户端类
客户机类是每一个其他类绑定到的类,是访问包含在各个类中的所有信息的主要方式,下面是该类中使用的变量和函数:
client.clientinfo # variable to access the ClientInfo Class [Object] client.hubinfo # variable to access the HubInfo Class[Object] client.nicklist # Variable to access the NickList class and the findbyusername function [Object] client.sid # The Client's Session ID given by the hub upon connecting [String] client.isconnected # Variable to check if the client is connected or not [Boolean] client.debug # Variable to turn on/off the debug feature, shows all raw data going in and out of the library, default is False [Boolean] client.events # Dictionary of events, as described above for the library to call at certain points. [Dictionary] client.connect() # Start connection function client.disconnect() # Immediately close connection client.sendmainchatmessage(message, emote=False) # Send a message to the main public chat, emote defaults to false, set to true to send as /me command client.sendprivatemessagebyclass(user, message, emote=False) # Send a private message to user, this uses the user object and not a string username, emote defaults to false, set to true to send as /me command client.sendprivatemessage(username, message, emote=False) # Send private message to username given, this uses the string username instead of the user object, emote defaults to false, set to true to send as /me command client.sendprivatemainchatmessagebyclass(user, message) # Send a mainchat message to a specific user (only seen by that user) using the user object client.sendprivatemainchatmessage(username, message) # Send a mainchat message to a specific user (only seen by that user) using the string username
用户类
库使用用户类跟踪中心中的所有用户,有多种方法获取用户类。onprivatemessage和onpublicmessage事件包括用户对象。如果您需要user对象,并且库只给了您用户名,请执行以下操作:
user = cl.nicklist.findbyusername(username)
如果未找到任何内容,则返回false;如果只找到一个匹配的内容,则返回单个用户对象。如果找到多个,则返回用户对象列表。
以下是可以在用户对象中找到的变量:
user.ip # IPv4 address (Currently IPv6 is not supported) [String] user.port # IPv4 Port [Integer] user.sharesize # Share Size in bytes [Integer] user.sharedfiles # Number of Shared Files [Integer] user.tag # Client Identification user.maxuploadspeed # Maximum Uplod Speed in bits/sec [Integer] user.openslots # Number of Open slots user has [Integer] user.autoslots # Automatic slot allocator speed limit in bytes/sec [Integer] user.maxopenslots # Maximum number of slots open in automatic slot manager mode [Integer] user.email # User's email [String] user.username # Username of user, only used for display purposes [String] user.description # User's description [String] user.hubsnormal # Number of hubs user is unregistered in [Integer] user.hubsreg # Number of hubs user is registered in [Integer] user.hubsop # Number of hubs user is op in [Integer] user.hubstatus # Protocol value of what users permissions are [Integer] see: http://adc.sourceforge.net/ADC.html#_inf (CT field) for more details user.sid # Session ID given to user from hub [String] user.cid # Client ID generated from Private ID of client [String] user.token # Token for Client to Client connections (Not yet supported) [String] user.isoperator # Variable if user is an operator in the hub or not [Boolean] user.ishidden # Variable is user is hidden in hub [Boolean] user.isaway # Variable if user is st to away [Boolean] user.isbot # Variable if user is a bot or not [Boolean] user.supportlist # List of protocols supported by client. [List] user.downloadlist # List of downloads (Currently not supported) [List]
hubinfo类
hubinfo类是一个非常小的类,它只存储hub发送的一些信息,它包含的内容如下:
hubinfo.hubsupports # List of protocols that the hub supports [List] hubinfo.hubname # Name of the hub [String] hubinfo.topic # Topic of the hub [String] hubinfo.hubversion # Version of the hub [String] hubinfo.lastmessage # Last message sent by the hub [String]
clientinfo类
clientinfo类是一个支持类,负责根据adc协议标准生成pid和cid,并查找运行脚本的计算机。它还负责保存以下信息:
clientinfo.hostname # The Hostname of the hub without scheme or port number [String] clientinfo.do_ssl # Variable on whether or not the address given to the hubaddress function requires ssl/tls [Boolean] clientinfo.port # port given to the hubaddress function [Integer] clientinfo.username # Username to connect with [String] clientinfo.password # Password to send upon connecting [String] clientinfo.description # Description to set for user connecting [String] clientinfo.email # Email of user clientinfo.reconnectondisconnect # If disconnected reconnect [Boolean] clientinfo.share # Share size in bytes (default is clientinfo.ShareSize.Empty) [Integer] clientinfo.followredirects # Follow redirects if any are given to the library [Boolean] clientinfo.respondtorevconnecttome # Respond to Reverse Connect To Me's, always false, no support for this option yet [Boolean] clientinfo.clientport # To be used later for client-to-client connections, currently defaults to 1337 [Integer] clientinfo.client_pid # Private ID for client [String] clientinfo.client_cid # Client ID for client [String] clientinfo.hubaddress() # Function to either get or set the Hub Address to connect to. e.g. addy = clientinfo.hubaddress() or clientinfo.hubaddress("adcs://somehub.com:500")