You are not logged in Log in Join
You are here: Home » Members » Amos's Zope Page » How To Publish Your Own Python Modules

Log in
Name

Password

 

How To Publish Your Own Python Modules

One little known fact about Zope is that it consists of a number of components which allow you to publish your own Python objects on the web without using the Zope management interface. This is different than writing a Zope Product--it is a more stripped down method of using object publishing, which will be familiar to folks who used to use "Bobo".

To publish your own objects with ZPublisher you need to find a way to get a web server talking with ZPublisher. The simplest way to do this is to use ZServer.

In the z2.py start script there is a comment which describes how to tell ZServer to publish your own Python module. The basic procedure is to create a zhhtp_handler and install it in the web server:

  zh = zhttp_handler(MODULE, '', HTTP_ENV)
  hs.install_handler(zh) # hs is a zhttp_server object

zhttp_handler takes three arguments:

  • a module name or module object -- This indicates the Python module to publish.
  • an optional prefix string -- This tells the web server where to locate the published module in its URL space. If you are only publishing one module, an empty string is a good choice for the prefix.
  • an optional environment dictionary -- This dictionary contains environment variable setting which will override default values.

z2.py comes configured to publish the Zope module. The simplest way to reconfigure z2.py to publish your own module is to comment out the part where z2.py publishes Zope and add in a stanza to publish your own module:

  #zh = zhttp_handler(MODULE, '', HTTP_ENV)
  #hs.install_handler(zh) # hs is a zhttp_server object

  sys.path.insert(0,'/path/to/my/module')    # add MyModule to the Python path
  my_handler = zhttp_handler('MyModule', '') # create a handler
  hs.install_handler(my_handler)             # install it in the http server

Now publish your module by running z2.py:

  z2.py -D -f '' -p '' -m '' -w 8080

This should start ZServer on port 8080 publishing your module. You can then access your published module through the web at:

  http://localhost:8080/