I have a Bash script which connects to MPD server and display current song and also accepts some keys to control the player. I have stripped some functions from it a few times when I needed to write a Bash script to do something with MPD, e.g. showing Last.fm playcount in Conky. A few times, I wanted to know what metadata of MPD command currentsong
provides, I quick hacked the script and made it printed out the returned data.
I think it's time to write a simple client to accept command and print out the response. Stripping that Bash script was my first thought, then I decided to write a Python script, instead. It's not because I prefer Python or something, just I hadn't written a single line Python code for a long time.
Before I showed you that Python code, there is one more thing I just found out. A really really simple client for MPD, which is telnet
tool.
$ telnet 0 6600 Trying 0.0.0.0... Connected to 0. Escape character is '^]'. OK MPD 0.15.0 currentsong file: A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3 Time: 224 Artist: Alexandria Maillot AlbumArtist: Alexandria Maillot Title: Just Another Girl Album: Just Another Girl Track: 1/4 Genre: Pop Composer: Alexandria Maillot and Joby Baker Pos: 1 Id: 159 OK stats artists: 56 albums: 56 songs: 392 uptime: 124 playtime: 54 db_playtime: 84859 db_update: 1291807454 OK pasus ACK [5@0] {} unknown command "pasus" pause OK play OK close Connection closed by foreign host.
You don't really need mpc
if you are a script nut with nc
(netcat) or expect
. If you don't need to process the response, you can always give commands to MPD, e.g.
echo "next" | telnet 0 6600
This will make MPD to play next song.
Now the Python code:
#!/usr/bin/env python
try:
import readline
except ImportError:
pass
import socket
import sys
HOST = 'localhost'
PORT = 6600
RECV_SIZE = 2**20 # One megabyte
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Connecting to %s:%d...' % (HOST, PORT)
s.connect((HOST, PORT))
except socket.error:
print 'Unable to connect.'
sys.exit(1)
print s.recv(RECV_SIZE)
print '*** <Control+C> to exit ***\n'
while True:
try:
cmd = raw_input(">>> ")
if cmd:
s.send(cmd + '\n')
if 'close' in cmd:
break
print s.recv(RECV_SIZE)
except KeyboardInterrupt:
break
print
s.close()
A sample interaction:
$ ./smpdc.py Connecting to localhost:6600... OK MPD 0.15.0 *** <Control+C> to exit *** >>> currentsong file: A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3 Time: 224 Artist: Alexandria Maillot AlbumArtist: Alexandria Maillot Title: Just Another Girl Album: Just Another Girl Track: 1/4 Genre: Pop Composer: Alexandria Maillot and Joby Baker Pos: 1 Id: 159 OK >>> playlist 0:A/A Lion Named Roar/Shoot It Straight.mp3 1:A/Alexandria Maillot/Just Another Girl/Just Another Girl.mp3 2:A/Ana Free/Keep On Walking.mp3 3:A/Ana Free/Questions In My Mind.mp3 4:S/Stars/Stars_Dead_Hearts.mp3 OK >>> next OK >>> currentsong file: A/Ana Free/Questions In My Mind.mp3 Time: 231 Artist: Ana Free AlbumArtist: Ana Free Title: Questions In My Mind Album: Radian Track: 1/5 Date: 2010 Genre: Pop Rock Composer: Ana Gomes Ferreira and Blake Harris Brandes Pos: 3 Id: 161 OK >>> close