The PyMOL HTTP server is a "pure" python module based off of the BaseHTTPServer module from the Python Standard Library.
For security reasons, PyMOL's HTTP server is currently restricted to only handling requests which originate on the local host (presumably from clicked links or JavaScript running inside an open browser window).
The source code is located at
$PYMOL_PATH/modules/web/pymolhttpd.py
,
and this file serves as a python module that can be imported by any
python script running inside of PyMOL.
Using PWG files to launch the PyMOL web server
The simplest way to start a web-service capable PyMOL, is for a user to follow a PyMOL Web GUI (PWG) link from a browser running on the local machine.
Running the PyMOL web server as the main pymol script
Alternatively, pymolhttpd.py can be used as the start-up script when you launch PyMOL.
pymol $PYMOL_PATH/modules/web/pymolhttpd.py
This will cause PyMOL to start serving requests on the default port (8080, assuming of course that the port is not already claimed by another application).
Running the PyMOL web server from your own Python scripts
The PyMOL web server module can be imported into and launched using code like this.
from pymol import pymolhttpd httpd = pymolhttpd.PymolHttpd(8080, "htdocs") httpd.start()This will cause PyMOL to respond to HTTP requests on port 8080. Furthermore, this server will also serve up ordinary web content from an "htdocs" folder in the current working directory.
PyMOL requests are in the standard URL syntax, for example:
http://localhost:8080/apply/pymol.cmd.load?filename=$TUT/1hpv.pdbFollowing this link from a browser will have the same effect as calling the PyMOL method:
cmd.load("$TUT/1hpv.pdb")
Exposing your own Python methods
If your script wishes to expose its own method to the web services API, it can do so by calling the "expose" method of the associated PymolHttpd instance. For example
def my_func(arg1,arg2,arg3="World!"): print arg1, arg2, arg3 httpd.expose("my_func", my_func)
will make it possible for your "my_func" method to be called via an HTTP request such as:
http://localhost:8080/apply/my_func?arg1=Hello&arg2=there
which would print
Hello there World!
into the PyMOL output window. You can thus use the PyMOL Web Services interface to build applications which include functionality beyond that of PyMOL itself.