简体   繁体   中英

Python script won't play sound as cgi, but via command line

The CGI python script shown below, sound.py, plays a sound through a speaker connected to a Raspberry's audio output. The sound plays fine when I execute the script from the command line via “python sound.py”. However, when I try to trigger it as a cgi script by invoking it via the Web ( http://192.168.1.246/cgi-bin/sound.py ), the sound won't play, and the browser will only display “Hi Hello World,” (which, I suppose. indicates that the server at least recognizes it as a CGI script and executes it without producing errors).

I thought that the issue might have to do with the permissions/ownership of the sound file and tinkered around with those, but that didn't help (I'm also pasting below information about the ownership and permission of the script itself, and the sound file).

Thanks a lot for helping!

Marc.

-rwxrwxrwx 1 root root 441 Jul 10 23:23 sound.py -rwxr-xr-x 1 root root 29812 Jul 9 22:58 cardinal-short.mp3

Script (sound.py):

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()

import pygame

name = "cardinal-short"
pygame.mixer.init()
pygame.mixer.music.load("/var/www/html/cgi-bin/cardinal-short.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(1.0)
while (pygame.mixer.music.get_busy() == True):
  continue
pygame.mixer.quit()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hi Hello World!"

I was able to fix this via:

sudo usermod -aG audio www-data

and then restarting apache.

The problem was that, and I quote from elsewhere on the web:

the sound devices (in /dev/snd/) are only accessible to members of group "audio". pi is a member of that group, and www-data is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM