I asked a similar question prior but i finally got further along in solving it, but now i am stuck.
I am running python on a webserver on dreamhost. I shell into it using putty and run my python script and it works great. But when i run it from php using exec, it crashes.
Using the passthru() function in php i get returned with this error:
Traceback (most recent call last): File "newwork.py", line 20,
in api=lastfm.Api(api_key) File "lastfm/api.py", line 22,
in __init__ File "lastfm/filecache.py", line 19,
in __init__ File "lastfm/filecache.py", line 76,
in _InitializeRootDirectory File "lastfm/filecache.py", line 70,
in _GetTmpCachePath File "lastfm/filecache.py", line 66,
in _GetUsername OSError: [Errno 22] Invalid argument
i have no clue what that means or where i should start. for more info i am using the python lastfm module found here: http://code.google.com/p/python-lastfm/
and my python script simply does this:
import sys
import lastfm
import MySQLdb
sys.stderr=sys.stdout
value="testuser"
api_key='---my api key is here'
api=lastfm.Api(api_key)
user=api.getUser(value)
top_artists=user.topArtists
any help would be greatly appreciated, or any ideas whatsoever. I dont even know where to start.
Edit: i did a little more wor开发者_开发百科k and found the full last line is:
line 66, in _GetUsername os.getlogin() or \ OSError: [Errno 22] Invalid argument
and the GetUsername function simply is this:
def _GetUsername(self):
'''Attempt to find the username in a cross-platform fashion.'''
return os.getenv('USER') or \
os.getenv('LOGNAME') or \
os.getenv('USERNAME') or \
os.getlogin() or \
'nobody'
with indentation corrected not here.
This previous answer about Errno 22 when using os.getlogin
should be enlightening:
From the
os.getlogin()
docs: "Returns the user logged in to the controlling terminal of the process." Your script does not have a controlling terminal when run from cron. The docs go on to suggest: "For most purposes, it is more useful to use the environment variableLOGNAME
to find out who the user is, orpwd.getpwuid(os.getuid())[0]
to get the login name of the currently effective user id."
You're calling the script via passthru()
in PHP. PHP will be running as the web server user -- frequently nobody
or apache
-- and like cron, that user will also not be control a terminal.
While you might be able to patch the code, you will probably be better served by filing a bug with the python-lastfm project with what you've uncovered. Unfortunately I don't know enough Python to help with that task, and also don't know enough about why the python-lastfm application wants to get the current user. It looks like it's just looking for a temporary file path and is trying to be clever about it.
An alternative to patching might be setting environment variables for the script, but there isn't an apparent way to do this for passthru
. Perhaps you can try using putenv('USER=myname')
before calling passthru
, where 'myname' is your shell login name.
精彩评论