1 Using the client library
2 =========================
4 The client library can be used as follows:
8 client = musicpd.MPDClient() # create client object
9 client.connect() # use MPD_HOST/MPD_PORT if set else
10 # test ${XDG_RUNTIME_DIR}/mpd/socket for existence
11 # fallback to localhost:6600
12 # connect support host/port argument as well
13 print(client.mpd_version) # print the mpd protocol version
14 print(client.cmd('one', 2)) # print result of the command "cmd one 2"
15 client.disconnect() # disconnect from the server
17 For a list of supported commands, their arguments (as MPD currently understands
18 them), and the functions used to parse their responses see :ref:`commands`.
20 See the `MPD protocol documentation`_ for more details.
22 Command lists are also supported using `command_list_ok_begin()` and
23 `command_list_end()` :
25 .. code-block:: python
27 client.command_list_ok_begin() # start a command list
28 client.update() # insert the update command into the list
29 client.status() # insert the status command into the list
30 results = client.command_list_end() # results will be a list with the results
32 Provide a 2-tuple as argument for command supporting ranges (cf. `MPD protocol documentation`_ for more details).
33 Possible ranges are: "START:END", "START:" and ":" :
35 .. code-block:: python
37 # An intelligent clear
38 # clears played track in the queue, currentsong included
39 pos = client.currentsong().get('pos', 0)
40 # the 2-tuple range object accepts str, no need to convert to int
41 client.delete((0, pos))
42 # missing end interpreted as highest value possible, pay attention still need a tuple.
43 client.delete((pos,)) # purge queue from current to the end
45 A notable case is the `rangeid` command allowing an empty range specified
46 as a single colon as argument (i.e. sending just ":"):
48 .. code-block:: python
50 # sending "rangeid :" to clear the range, play everything
51 client.rangeid(()) # send an empty tuple
53 Empty start in range (i.e. ":END") are not possible and will raise a CommandError.
56 Commands may also return iterators instead of lists if `iterate` is set to
59 .. code-block:: python
62 for song in client.playlistinfo():
65 Each command have a *send\_<CMD>* and a *fetch\_<CMD>* variant, which allows to
66 send a MPD command and then fetch the result later.
67 This is useful for the idle command:
69 .. code-block:: python
71 >>> client.send_idle()
72 # do something else or use function like select()
73 # http://docs.python.org/howto/sockets.html#non-blocking-sockets
74 # ex. select([client], [], [])
75 >>> events = client.fetch_idle()
77 # more complex use for example, with glib/gobject:
78 >>> def callback(source, condition):
79 >>> changes = client.fetch_idle()
81 >>> return False # removes the IO watcher
83 >>> client.send_idle()
84 >>> gobject.io_add_watch(client, gobject.IO_IN, callback)
85 >>> gobject.MainLoop().run()
88 .. _MPD protocol documentation: http://www.musicpd.org/doc/protocol/