问题描述:

I'm currently hosting a SimpleHTTPServer locally and have cd'd it into my server folder. I'm trying to use afplay to get it to play audio that is currently located in the server folder. This is my code:

import subprocess

import os

import urllib

import urllib2

urllib.urlretrieve('http://0.0.0.0:8000', r'Daybreak.mp3')

return_code = subprocess.call(["afplay", audio_file])

The file is located in the folder the server is currently hosting from and I am getting GET requests in Terminal. This is the error I get:

 File "<stdin>", line 1, in <module>

File "<string>", line 11, in <module>

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 94, in urlretrieve

return _urlopener.retrieve(url, filename, reporthook, data)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 244, in retrieve

tfp = open(filename, 'wb')

IOError: [Errno 13] Permission denied: 'Daybreak.mp3'

I'm not sure why permission is denied as I have got read and write access to the folder.

网友答案:

You might have got read and write access to the folder. But you are not specifying your access credentials in the python script. Python has no way of knowing that you are authorized.

You may want to use some authorization method, like HTTPPasswordMgrWithDefaultRealm().

serverUrl = "..."
ID = "..."
accessToken = "..."


p = urllib2.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, serverUrl, ID, accessToken)
handler = urllib2.HTTPBasicAuthHandler(p)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
req = urllib2.urlretrieve()
相关阅读:
Top