|
UsingPyelaNet
How to use the Pyela-net API.
Using pyelaThe following information is technical in nature, explaining the Pyela API and its uses. Connecting to an Eternal Lands ServerHere's an example of using Pyela from within a Python prompt. From the directory you extracted Pyela to, start a Python prompt, then do the following: >>> from pyela.el.net.connections import ELConnection
>>> elc = ELConnection('username', 'password')
>>> elc.connect()
True
>>>Easy, isn't it? You can also pass host and port values when constructing an instance of ELConnection, like so: >>> from pyela.el.net.connections import ELConnection
>>> elc = ELConnection('username', 'password', host='my.host.com', port=1234)
>>> elc.connect()In the event of an error whilst connecting, an exception will be thrown: pyela.el.common.exceptions.ConnectionException. This will contain a user-friendly error message, so you should know what's wrong immediately. I highly recommend that you use the help() function and pass it an ELConnection, as this will give you further details on the use of the object, as well as what methods/functions and attributes are available. Sending PacketsThis would be about the right time to introduce the ELPacket object, which is just a simple wrapper for the message type and data relating to any one message from the server. See the Eternal Lands Network Protocol Documentation for a description of the message format. The send function in the ELConnection class takes one parameter, an instance of ELPacket, and constructs a message from its contents then sends it using the underlying socket to the EL server it's connected to. For sending information to the server, that's all you need to do. Here's an example: >>> from pyela.el.net.packets import ELPacket >>> elp = ELPacket(ELNetToServer.RAW_TEXT, "#gm Hi there!\0") >>> elc.send(elp) # elc is the instance of ELConnection we created earlier Hot stuff, I know. Receiving Data From a ConnectionThe recv() method on an instance of ELConnection will return a list of pyela.el.net.packets.ELPacket instances. These contain a type attribute (int) that's relative to what type of message this is, and a data attribute (string) that contains the information for this particular packet. >>> packets = elc.recv() >>> for packet in packets: >>> # do something with this tasty packet |
Sign in to add a comment