OSError:[Errno 22]Python PHP问题

2024-05-21 04:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前问过一个类似的问题,但我最终还是解决了这个问题,但现在我被卡住了。在

我正在dreamhost上的web服务器上运行python。我用putty编写了它,并运行了python脚本,它工作得很好。但是当我使用exec从php运行它时,它崩溃了。在

在php中使用passthru()函数时,返回以下错误:

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 

我不知道那意味着什么,也不知道该从哪里开始。关于更多信息,我使用的是python lastfm模块:http://code.google.com/p/python-lastfm/

我的python脚本只执行以下操作:

^{pr2}$

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

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

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'

这里没有修正压痕。在


Tags: or函数inpy脚本apiinitos
1条回答
网友
1楼 · 发布于 2024-05-21 04:33:22

This previous answer about Errno 22 when using ^{}应该很有启发性:

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服务器用户的身份频繁运行nobody或{}和cron一样,该用户也不会控制终端。在

虽然您可能能够修补代码,但是将您发现的东西归档到pythonlastfm项目中可能会更好地为您服务。不幸的是,我不知道足够的Python来帮助完成这项任务,也不知道pythonlastfm应用程序为什么要获得当前用户。它的看起来像是在寻找一个临时的文件路径,并试图巧妙地利用它。在

修补程序的另一种方法可能是为脚本设置环境变量,但对于passthru没有一种明显的方法来完成这一操作。也许您可以在调用passthru之前尝试使用^{},其中'myname'是您的shell登录名。在

相关问题 更多 >