Now that we've written an application, let's distribute it across a network. The BookServer runs on one computer and the client program on another.
Distributed COM does it all for you. You don't have to change a single line of your Python code. All you need is to enable a few settings on the server (which are not specific to this application, but to DCOM in general) and to change one line in the client code. Here are the steps:
1. DCOM needs to be enabled on the remote server. Choose Start ® Run and type dcomcnfg.exe. A configuration dialog starts up. Select the Default Properties tab and check the box for Enable Distributed COM on this computer.
2. Python and PythonWin should be installed properly on the server, and the Doubletalk COM server should be registered.
3. In our Visual Basic program, go to the line of code where we create the BookServer, and add a parameter for the remote machine name. We added an extra box in the Options dialog, the contents of which are stored in the variable RemoteMachine. Here's the startup code:
Set BookServer = CreateObject ("Doubletalk.BookServer", RemoteMachine)
If the string RemoteMachine is empty, the local server starts up. Otherwise, DCOM talks to the remote machine, and the BookServer starts up there. All calls to the COM object and any return values (including large arrays of data) are automatically packaged and transported across the network.
To demonstrate this, we created a data file called remotedata.dtj on a machine called yosemite and ran the client on a different machine. In the Options box input the name yosemite and hit the Reload button. At this point, the COM server starts on the remote machine. If you don't get an error message, it's connected. Then, in the File ® Open dialog, type the path of the file on yosemite (you can't browse for it as it isn't on the client system), and the remote file loads. Note the window caption in Figure 11-1.
Figure 11-1. Remote server with one line of code |
The length of this chapter speaks for itself. Distributed COM lets you build distributed network applications in Python without changing a line of code.
There is of course a great deal more to DCOM than this. More can be done, and there are also a number of caveats about machine configuration. In particular, designing a robust multiuser server architecture is a complex subject in any language; it will almost certainly involve COM singletons and either threads or an event loop for handling multiple simultaneous connections. Chapter 12, Advanced Python and COM, provides detailed instructions for configuring DCOM for both Python clients and servers and explains the background to the application we just distributed.