Connecting to Servers

Your primary dealings when connecting to a server will be with the Connection class

class minecraft.networking.connection.Connection(address, port=25565, auth_token=None, username=None, initial_version=None, allowed_versions=None, handle_exception=None)[source]

This class represents a connection to a minecraft server, it handles everything from connecting, sending packets to handling default network behaviour

Sets up an instance of this object to be able to connect to a minecraft server.

The connect method needs to be called in order to actually begin the connection

  • address – address of the server to connect to
  • port(int) – port of the server to connect to
  • auth_tokenminecraft.authentication.AuthenticationToken object. If None, no authentication is attempted and the server is assumed to be running in offline mode.
  • username – Username string; only applicable in offline mode.
  • initial_version – A Minecraft version string or protocol version number to use if the server’s protocol version cannot be determined. (Although it is now somewhat inaccurate, this name is retained for backward compatibility.)
  • allowed_versions – A set of versions, each being a Minecraft version string or protocol version number, restricting the versions that the client may use in connecting to the server.
  • handle_exception – A function to be called when an exception occurs in the client’s networking thread, taking 2 arguments: the exception object ‘e’ as in ‘except Exception as e’, and a 3-tuple given by sys.exc_info(); or None for the default behaviour of raising the exception from its original context; or False for no action. In any case, the networking thread will terminate, the exception will be available via the ‘exception’ and ‘exc_info’ attributes of the ‘Connection’ instance.

Attempt to begin connecting to the server. May safely be called multiple times after the first, i.e. to reconnect.


Terminate the existing server connection, if there is one.

register_packet_listener(method, *args)[source]

Registers a listener method which will be notified when a packet of a selected type is received

  • method – The method which will be called back with the packet
  • args – The packets to listen for
status(handle_status=None, handle_ping=False)[source]

Issue a status request to the server and then disconnect.

  • handle_status – a function to be called with the status dictionary None for the default behaviour of printing the dictionary to standard output, or False to ignore the result.
  • handle_ping – a function to be called with the measured latency in milliseconds, None for the default handler, which prints the latency to standard outout, or False, to prevent measurement of the latency.
write_packet(packet, force=False)[source]

Writes a packet to the server.

If force is set to true, the method attempts to acquire the write lock and write the packet out immediately, and as such may block.

If force is false then the packet will be added to the end of the packet writing queue to be sent ‘as soon as possible’

  • packet – The network.packets.Packet to write
  • force(bool) – Specifies if the packet write should be immediate

Writing Packets

The packet class uses a lot of magic to work, here is how to use them. Look up the particular packet you need to deal with, for this example let’s go with the

class, **kwargs)[source]
definition = [{'keep_alive_id': <class 'minecraft.networking.types.VarInt'>}]

Pay close attention to the definition attribute, and how our class variable corresponds to the name given from the definition:

from minecraft.networking.packets import serverbound
packet =
packet.keep_alive_id = random.randint(0, 5000)

and just like that, the packet will be written out to the server.

It is possible to implement your own custom packets by subclassing minecraft.networking.packets.Packet. Read the docstrings and in and follow the examples in its subpackages for more details on how to do advanced tasks like having a packet that is compatible across multiple protocol versions.

Listening for Certain Packets

Let’s look at how to listen for certain packets, the relevant method being

Connection.register_packet_listener(method, *args)[source]

Registers a listener method which will be notified when a packet of a selected type is received

  • method – The method which will be called back with the packet
  • args – The packets to listen for

An example of this can be found in the headless client, it is recreated here:

connection = Connection(options.address, options.port, auth_token=auth_token)

def print_chat(chat_packet):
    print "Position: " + str(chat_packet.position)
    print "Data: " + chat_packet.json_data

from import ChatMessagePacket
connection.register_packet_listener(print_chat, ChatMessagePacket)

The field names position and json_data are inferred by again looking at the definition attribute as before

class, **kwargs)[source]
definition = [{'json_data': <class 'minecraft.networking.types.String'>}, {'position': <class 'minecraft.networking.types.Byte'>}]