Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Python Cookbook 2Nd Edition Jun 1002005 [Electronic resources] - نسخه متنی

David Ascher, Alex Martelli, Anna Ravenscroft

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Recipe 15.5. Implementing SimpleXMLRPCServer Niceties


Credit: Rune Hansen


Problem


You
are coding XML-RPC servers with the Python Standard Library
SimpleXMLRPCServer class and want to ensure
you're using the simple but useful idioms that can
ease your coding, or give your servers more flexibility at no
substantial cost to you.


Solution


Here are a few tweaks I generally use, to enhance my
servers' usability, when I'm
developing servers based on SimpleXMLRPCServer:

# give the base class a short, readable nickname
from SimpleXMLRPCServer import SimpleXMLRPCServer as BaseServer
class Server(BaseServer):
def _ _init_ _(self, host, port):
# accept separate hostname and portnumber and group them
BaseServer._ _init_ _(self, (host, port))
def server_bind(self):
# allow fast restart of the server after it's killed
import socket
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
BaseServer.server_bind(self)
allowedClientHosts = '127.0.0.1', '192.168.0.15',
def verify_request(self, request, client_address):
# forbid requests except from specific client hosts
return client_address[0] in self.allowedClientHosts


Discussion


The recipe begins with a statement of the form
from module import
name as
nickname, a Python idiom that is often
handy for importing something under a short and usable nickname.
It's certainly miles better than having to
repeatedly write
SimpleXMLRPCServer.SimpleXMLRPCServer after a
simple import statement, or using the ill-advised
construct from module
import *, which mixes up all the namespaces and can often cause
subtle and hard-to-find bugs.

The sole purpose of the _ _init_ _ statement of
class Server is to accept host and
port as separate parameters and group them into the
required tuple. I find myself often writing such statements with the
many Python functions and classes that require this
address tuple grouping (your tastes, of
course, may be different).

By default, a server socket belonging to a process that dies is kept
busy for quite a while. Particularly during development, it is handy
to kill such a process, edit the script, and restart immediately. For
such an immediate restart to work, you must ensure the code of your
server sets the SO_REUSEADDR option on the
relevant socket, as the recipe's code does in its
overridden method server_bind.

Last but not least, the recipe overrides
verify_request in order to apply a simple check
that refuses service except to requests coming from client hosts on a
predefined list. This approach doesn't provide
rock-solid security, but nevertheless, it is potentially useful.
Again, it's particularly useful during development,
to help avoid those cases where some other developer on the same LAN
accidentally connects his client to the server I'm
just developing, and we both experience puzzling problems until we
figure out what's happened!


See Also


The SimpleXMLRPCServer module is part of the
Python Standard Library and is documented in a chapter of the
Library Reference portion of
Python's online documentation.


/ 394