Package: zeroframe-ws-client
ZeroFramePyZeroFrame WebSocket API for Python.DescriptionThis is Python WebSocket client forZeroFrame API. It supports (almost) same features as default ZeroFrame that is included in ZeroNet sites, but it is using WebSocket client so it can be used in local programs.InstallationRequirementsContentHash requires Python 3.5 or higher.From PyPIThe recommended way to install ContentHash is from PyPI with PIP.pipinstallzeroframe-ws-clientFrom SourceAlternatively, you can also install it from the source.gitclonehttps://github.com/filips123/ZeroFramePy.gitcdZeroFramePy
pythonsetup.pyinstallUsageImporting PackageYou can import ZeroFrameJS fromzeroframe_ws_clientpackage.fromzeroframe_ws_clientimportZeroFrameCreating ConnectionTo create a connection, you need to specify the ZeroNet site address.zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')If ZeroNet instance is usingMultiuserplugin and you want to use specific account, you need to specify a master address of the account you want to use. Account must already exist on the instance.zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',multiuser_master_address='1Hxki73XprDRedUdA3Remm3kBX5FZxhFR3')If you want to create a new account, you also need to specify a master seed of it. Note that this feature is unsafe on the untrusted proxy.zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',multiuser_master_address='1KAtuzxwbD1QuMHMuXWcUdoo5ppc5wnot9',multiuser_master_seed='fdbaf75427ba69a3d4aa8e19372e05879e9e2d866e579dd30be25e6fab7e3fb2')If needed, you can also specify protocol, host and port of ZeroNet instance.zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',instance_host='192.168.1.1',instance_port=8080,instance_secure=True)Log and error message fromzeroframe.logandzeroframe.errorwill not be displayed by default. If you want to, you can also display them as debug info.zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',show_log=True,show_error=True)By default, the client will try to reconnect WebSocket if the connection was closed every 5 seconds. You can also configure time delay and total attempts. Delay is specified in milliseconds. The number of attempts-1means infinity and0means zero (disabled reconnecting).zeroframe=ZeroFrame('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D',reconnect_attempts=10,reconnect_delay=1000)The client will then obtain wrapper key to the site and connect to WebSocket using it.You can now normally use ZeroFrame API. Just remember that there is no wrapper, so wrapper commands are not available. The client is connected directly to the WebSocket server, so you need to use its commands.Note that the WebSocket server sometimes sends commands (notification,progress,error,prompt,confirm,setSiteInfo,setAnnouncerInfo,updating,redirect,injectHtml,injectScript) that are normally handled by the wrapper. Because there is no wrapper, you need to handle those commands yourself if needed. Commandsresponseandpingare already handled by this client so you don't need to handle them.Sending CommandYou can use thecmdmethod to issue commands.zeroframe.cmd('siteInfo',{},lambdaresult:print(result))You can also use thecmdpmethod to get results as Python asyncio futures.result=zeroframe.cmd('siteInfo',{})print(result.result())Sending ResponseTo submit responses, you need to useresponsecommand.zeroframe.response(10,'Hello World')Logging InformationThere are alsologanderrormethods which are available for logging. They will display output to console if enabled.zeroframe.log('Connected')zeroframe.error('Connection failed')Handling ConnectionThere are also public handler methods which you can overwrite to add your own logic to ZeroFrame.classZeroApp(ZeroFrame):defon_request(self,cmd,message):ifcmd=='helloWorld':self.log('Hello World')defon_open_websocket(self):self.log('Connected to WebSocket')defon_error_websocket(self,error):self.error('WebSocket connection error')defon_close_websocket(self):self.error('WebSocket connection closed')Closing ConnectionYou can useclosemethod to close WebSocket connection to ZeroFrame API.zeroframe.close()Calling Commands DirectlyYou can also directly call commands via__getattr__method. Command name is accepted as an object's property and parameters are accepted as a method's arguments. Command returnsasyncio.Futurewith the result.Command with no arguments can be accessed withzeroframe.cmdName().Command with keyword arguments can be accessed withzeroframe.cmdName(key1=value1, key2=value2).Command with normal arguments can be accessed withzeroframe.cmdName(value1, value2).siteInfo=zeroframe.siteInfo()print(siteInfo.result())Other ExamplesYou could also look toexample.py.VersioningThis library usesSemVerfor versioning. For the versions available, seethe tagson this repository.LicenseThis library is licensed under the MIT license. See theLICENSEfile for details.