2011-03-16 66 views
1

我之前问过类似的问题,但我终于解决了它,但现在我卡住了。OSError:[Errno 22] Python PHP问题

我在Dreamhost上的web服务器上运行python。我使用putty将其壳化,然后运行我的python脚本,效果很好。但是当我使用exec从php运行它时,它崩溃了。

使用PHP时得到与此错误返回的中继()函数:

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 

我不知道这意味着什么或者我应该开始。更多信息我使用这里找到蟒蛇LastFM等模块:http://code.google.com/p/python-lastfm/

和我的Python脚本简单地做到这一点:

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 

任何帮助,将不胜感激,或不得以任何想法。我甚至不知道从哪里开始。

编辑:我做了一些更多的工作,发现全最后一行是:

line 66, in _GetUsername os.getlogin() or \ OSError: [Errno 22] Invalid argument 

和GetUsername功能简单说就是:

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' 

缩进修正不在这里。

回答

0

This previous answer about Errno 22 when using os.getlogin应该有所启发:

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 variable LOGNAME to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the login name of the currently effective user id."

您要通过在PHP passthru()调用脚本。 PHP将作为Web服务器用户运行 - 频繁地为nobodyapache - 并且与cron一样,该用户也不会控制终端。

尽管您可能可以修补代码,但您可能会更好地服务于您使用python-lastfm项目发现了一个bug。不幸的是,我不知道有足够的Python来帮助完成这项任务,并且也不太了解python-lastfm应用程序想要获取当前用户的原因。它看起来像像它只是寻找一个临时文件路径,并试图对它很聪明。

修补的替代方法可能是为脚本设置环境变量,但是对于passthru没有明显的方法来执行此操作。也许您可以在致电passthru之前尝试使用putenv('USER=myname'),其中'myname'是您的shell登录名。