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('foo', 42)) # print result of the request "cmd foo 42"
15 # (nb. for actual command, see link to the protocol below)
16 client.disconnect() # disconnect from the server
18 In the example above `cmd` in not an actual MPD command, for a list of
19 supported commands, their arguments (as MPD currently understands
20 them), and the functions used to parse their responses see :ref:`commands`.
22 See the `MPD protocol documentation`_ for more details.
24 Command lists are also supported using `command_list_ok_begin()` and
25 `command_list_end()` :
27 .. code-block:: python
29 client.command_list_ok_begin() # start a command list
30 client.update() # insert the update command into the list
31 client.status() # insert the status command into the list
32 results = client.command_list_end() # results will be a list with the results
34 Provide a 2-tuple as argument for command supporting ranges (cf. `MPD protocol documentation`_ for more details).
35 Possible ranges are: "START:END", "START:" and ":" :
37 .. code-block:: python
39 # An intelligent clear
40 # clears played track in the queue, currentsong included
41 pos = client.currentsong().get('pos', 0)
42 # the 2-tuple range object accepts str, no need to convert to int
43 client.delete((0, pos))
44 # missing end interpreted as highest value possible, pay attention still need a tuple.
45 client.delete((pos,)) # purge queue from current to the end
47 A notable case is the `rangeid` command allowing an empty range specified
48 as a single colon as argument (i.e. sending just ":"):
50 .. code-block:: python
52 # sending "rangeid :" to clear the range, play everything
53 client.rangeid(()) # send an empty tuple
55 Empty start in range (i.e. ":END") are not possible and will raise a CommandError.
58 Commands may also return iterators instead of lists if `iterate` is set to
61 .. code-block:: python
64 for song in client.playlistinfo():
67 Each command have a *send\_<CMD>* and a *fetch\_<CMD>* variant, which allows to
68 send a MPD command and then fetch the result later.
69 This is useful for the idle command:
71 .. code-block:: python
73 >>> client.send_idle()
74 # do something else or use function like select()
75 # http://docs.python.org/howto/sockets.html#non-blocking-sockets
76 # ex. select([client], [], [])
77 >>> events = client.fetch_idle()
79 # more complex use for example, with glib/gobject:
80 >>> def callback(source, condition):
81 >>> changes = client.fetch_idle()
83 >>> return False # removes the IO watcher
85 >>> client.send_idle()
86 >>> gobject.io_add_watch(client, gobject.IO_IN, callback)
87 >>> gobject.MainLoop().run()
90 .. _MPD protocol documentation: http://www.musicpd.org/doc/protocol/