1

我之前问过一个类似的问题,但我终于在解决它方面取得了进一步的进展,但现在我被卡住了。

我在 Dreamhost 的网络服务器上运行 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脚本只是这样做:

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'

缩进不在这里更正。

4

1 回答 1

1

使用时有关 Errno 22 的先前答案os.getlogin应该很有启发性:

来自os.getlogin() 文档:“将登录的用户返回到进程的控制终端。” 从 cron 运行时,您的脚本没有控制终端。文档继续建议:“对于大多数目的,使用环境变量LOGNAME来找出用户是谁,或者pwd.getpwuid(os.getuid())[0]获取当前有效用户 ID 的登录名更有用。”

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

虽然您可能能够修补代码,但使用您发现的内容向 python-lastfm 项目提交错误可能会更好地为您服务。不幸的是,我没有足够的 Python 知识来帮助完成这项任务,而且我也不知道为什么 python-lastfm 应用程序想要获取当前用户。看起来它只是在寻找一个临时文件路径,并试图变得聪明。

修补的替代方法可能是为脚本设置环境变量,但对于passthru. putenv('USER=myname')也许您可以在调用之前尝试使用passthru,其中 'myname' 是您的 shell 登录名。

于 2011-03-16T07:01:57.223 回答