0

I would like to check internet connexion from my plone site. I tried a ping in a python script

## 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)

I got these errors :

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
------
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
user1499220
  • 419
  • 4
  • 12
  • 23

2 Answers2

3

Python Scripts in Zope are sandboxed (via RestrictedPython, which means that any module imports have to be declared safe first. Adding modules to the declared-safe list is generally a Bad Idea unless you know what you are doing.

To declare a module as importable into Python Scripts, you'll need to create a python package, then add the following code to it so it is executed when Zope starts:

from Products.PythonScripts.Utility import allow_module

allow_module('ping')

This'll allow any import from that module (use with caution)!

It's better to allow only specific methods and classes from a module; use a ModuleSecurity declaration for that:

from AccessControl import ModuleSecurityInfo

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

This is documented in the Security chapter of the Zope Developers Guide, specifically the section on module security assertions.

Note that it nearly always is a better idea to do all this work in a tightly constrained method in unrestricted code (e.g. a regular python package), then allow that method to be used from a python script instead.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you for your answer. Actually, what I try to do is a SOAP connection. I try ping just to check that I have access to internet. – user1499220 Jul 23 '12 at 15:59
1

It won't work.

You CANNOT import arbitrary Python modules in RestrictedPython scripts, as in the answer you were told yesterday:

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

If you need to use arbitraty Python modules you need to write your own Plone add-on for that and use a BrowserView for the purpose. RestrictedPython through-the-web-browser development is not enough:

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

Community
  • 1
  • 1
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • Thank you for your answer. I just began with Plone/Zope and also Python so I don't really understand how can I write my own add-on. I neither don't understand what do you call "an arbitrary Python module" . If you know any tutorial which can help me ... – user1499220 Jul 20 '12 at 10:29
  • 1
    You should master Python basics before you can efficiently customize Plone. If you are very new to Python, Plone or software development generally it is suggested that you read Professional Plone 4 Development book before you attempt to develop your own solutions. https://plone.org/documentation Make sure you also master generic Python before moving to Plone http://collective-docs.readthedocs.org/en/latest/getstarted/python.html – Mikko Ohtamaa Jul 20 '12 at 11:08
  • 2
    Actually, you *can* import arbitrary python modules *if you declare them as safe first*. – Martijn Pieters Jul 22 '12 at 10:11