2011-03-16 67 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登錄名。