2012-07-20 19 views
0

我想從我的plone站點檢查internet connexion。我想在一個python腳本在Plone的RestrictedPython腳本中導入ping模塊

## Script (Python) "pwreset_action.cpy" 
##bind container=container 
##bind context=context 
##bind namespace= 
##bind script=script 
##bind subpath=traverse_subpath 
##title=Reset a user's password 
##parameters=randomstring, userid=None, password=None, password2=None 
from Products.CMFCore.utils import getToolByName 
from Products.PasswordResetTool.PasswordResetTool import InvalidRequestError, ExpiredRequestError 
import ping, socket 



status = "success" 
pw_tool = getToolByName(context, 'portal_password_reset') 
try: 
    pw_tool.resetPassword(userid, randomstring, password) 

except ExpiredRequestError: 
    status = "expired" 
except InvalidRequestError: 
    status = "invalid" 
except RuntimeError: 
    status = "invalid" 

context.plone_log("TRYING TO PING") 
try : 



ping.verbose_ping('www.google.com' , run=3) 
    context.plone_log("PING DONE") 
except socket.error, e: 
    context.plone_log("PING FAILED") 


return state.set(status=status) 

一個ping我得到這些錯誤:

2012-07-20T11:37:08 INFO SignalHandler Caught signal SIGTERM 
------ 
2012-07-20T11:37:08 INFO Z2 Shutting down fast 
------ 
2012-07-20T11:37:08 INFO ZServer closing HTTP to new connections 
------ 
2012-07-20T11:37:42 INFO ZServer HTTP server started at Fri Jul 20 11:37:42 2012 
    Hostname: 0.0.0.0 
    Port: 8080 
------ 
2012-07-20T11:37:42 WARNING SecurityInfo Conflicting security declarations for "setText" 
------ 
2012-07-20T11:37:42 WARNING SecurityInfo Class "ATTopic" had conflicting security declarations 
------ 
2012-07-20T11:37:46 INFO plone.app.theming Patched Zope Management Interface to disable theming. 
------ 
2012-07-20T11:37:48 INFO PloneFormGen Patching plone.app.portlets ColumnPortletManagerRenderer to not catch Retry exceptions 
------ 
2012-07-20T11:37:48 INFO Zope Ready to handle requests 
------ 
+0

您包含正常的日誌輸出,沒有錯誤。 :-) – 2012-07-22 10:16:03

回答

2

Zope中的Python腳本的沙盒(通過RestrictedPython,這意味着任何模塊導入必須聲明安全第一添加模塊來聲明。 - 安全列表通常是一個壞主意,除非你知道你在做什麼。

要聲明一個模塊可導入到Python腳本中,你需要創建一個python包,然後添加下面的代碼,所以它是當Zope啓動時執行:

from Products.PythonScripts.Utility import allow_module 

allow_module('ping') 

這將允許任何從該模塊導入(謹慎使用)!

最好只允許模塊中的特定方法和類;使用一個ModuleSecurity聲明:

from AccessControl import ModuleSecurityInfo 

ModuleSecurityInfo('ping').declarePublic('verbose_ping') 
ModuleSecurityInfo('socket').declarePublic('error') 

這是記錄在Security chapter of the Zope Developers Guide,特別是section on module security assertions

注意,它幾乎總是一個更好的主意,這樣做在無限制的代碼中的嚴格限制方法(例如常規的Python包),所有這些工作,然後讓是從一個Python腳本中使用方法來代替。

+0

謝謝你的回答。實際上,我試圖做的是一個SOAP連接。我嘗試ping只是爲了檢查我是否可以訪問互聯網。 – user1499220 2012-07-23 15:59:39

2

它不會工作。

不能RestrictedPython腳本進口任意的Python模塊,在回答你被告知昨天:

https://stackoverflow.com/a/11568316/315168

如果你需要使用arbitraty你需要編寫自己的Plone添加Python模塊 - 爲此,並使用BrowserView的目的。 RestrictedPython透Web瀏覽器的開發是不夠的:

http://collective-docs.readthedocs.org/en/latest/getstarted/index.html

+0

謝謝你的回答。我剛剛開始使用Plone/Zope和Python,所以我不太瞭解如何編寫自己的附加組件。我也不明白你稱之爲「一個任意的Python模塊」。如果你知道任何可以幫助我的教程... – user1499220 2012-07-20 10:29:13

+1

你應該掌握Python基礎知識,然後纔能有效地自定義Plone。如果您對Python,Plone或軟件開發非常陌生,建議您在嘗試開發自己的解決方案之前閱讀Professional Plone 4 Development書。 https://plone.org/documentation在移植到Plone之前,確保你也掌握了通用的Python http://collective-docs.readthedocs.org/en/latest/getstarted/python.html – 2012-07-20 11:08:53

+1

實際上,你可以導入任意python模塊*如果你聲明它們是安全的*。 – 2012-07-22 10:11:23