Archive for the ‘Python’ Category

XMLRPC Server Slow in Python – How to Fix

Friday, February 19th, 2010

Background:
I had set up a Python XML-RPC server on one machine. When accessing the web services from other machines it would sometimes take up to 20 seconds to get a response. The strange thing was that this only happened when accessing the web service from some machines but not others.

Problem and Solution:
It turns out that Python’s BaseHTTPRequestHandler was trying to log the fully qualified domain name of each request’s IP address. Thus when we connected from machines that didn’t have a fully qualified domain name, it would take a long time to not find the FQDN. There is actually a bug for this.

Below is a simple fix that solved the problem for me (in Python 2.4). This would probably work in future versions too.

Assume this is your basic XMLRPC Server set up in Python:

import SimpleXMLRPCServer

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()


Here is the fixed version. I added in an override for the trouble function:

import SimpleXMLRPCServer

#new code
import BaseHTTPServer
def not_insane_address_string(self):
    host, port = self.client_address[:2]
    return '%s (no getfqdn)' % host #used to call: socket.getfqdn(host)
BaseHTTPServer.BaseHTTPRequestHandler.address_string = \
    not_insane_address_string
#end new code

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()

Making py2exe Work with Paramiko

Friday, June 26th, 2009

When running py2exe with a script importing Paramiko I noticed this in the output:

The following modules appear to be missing ... paramiko ...

And then in the log file created when I run the exe I saw this:

Traceback (most recent call last):
 ...
ImportError: No module named paramiko

This has to do with py2exe not being able to handle egg files. So what I did is pulled out the paramiko directory from the egg, and placed that in my site-packages directory, and then deleted the egg. It works now though the setup function still tells me this:

The following modules appear to be missing
['Crypto.PublicKey._fastmath', 'Crypto.Util.winrandom', '_ssl', 'r_hmac']

Luckily as far as I can tell, I don’t use those modules anywhere.

This guy had the same problem and
made some sort of programmatic solution
but it didn’t work for me. Since when I ran the setup function, it didn’t throw an ImportError.

Tips for Using the Paramiko SFTP Client

Friday, June 19th, 2009

The Paramiko library seems to be the best way to do an SFTP client in Python.

Here is a good example of set up and usage.

However in connecting to a WS FTP server, I was still hitting some problems.

When I tried to do:

sftp.get(remotepath='setup.py',localpath='setup.py')

I got:

*** IOError: Folder not found: setup.py

and when I tried:

sftp.sftp.get(remotepath='./setup.py',localpath='setup.py')

I got:

*** IOError: [Errno 13] Permission Denied!

It turns out the trick for working with WS FTP Server, you need to reference your files as current_directory/filename. (no idea why!)

So the correct command is:

sftp.sftp.get(remotepath='greg/setup.py',localpath='setup.py')

where greg is my current working directory (yes, even though I’m already in that directory!)

PyCrypto – Fixing Setup script exited with error: The .NET Framework SDK needs to be instal led before building extensions for Python

Thursday, June 18th, 2009

So here’s this horrible thing that happening to me when trying to install Paramiko on Windows XP:

C:\Documents and Settings\gpinero>easy_install paramiko
Searching for paramiko
Best match: paramiko 1.7.4
Processing paramiko-1.7.4-py2.4.egg
paramiko 1.7.4 is already the active version in easy-install.pth

Using c:\python24\lib\site-packages\paramiko-1.7.4-py2.4.egg
Processing dependencies for paramiko
Searching for pycrypto>=1.9
Reading http://pypi.python.org/simple/pycrypto/
Reading http://pycrypto.sourceforge.net
Reading http://www.amk.ca/python/code/crypto
Best match: pycrypto 2.0.1
Downloading http://www.amk.ca/files/python/crypto/pycrypto-2.0.1.tar.gz
Processing pycrypto-2.0.1.tar.gz
Running pycrypto-2.0.1\setup.py -q bdist_egg --dist-dir c:\docume~1\gpinero\loca
ls~1\temp\easy_install-gpdsc5\pycrypto-2.0.1\egg-dist-tmp-8dcze9
error: Setup script exited with error: The .NET Framework SDK needs to be instal
led before building extensions for Python.

I heard it’s a bear to install the .NET framework SDK, and supposedly the install still won’t work. So what you do instead is download this prebuilt pycrypto binary from here, and install it.

Stupid Python Tricks – Unescape a String

Friday, June 5th, 2009

Ever been given a pre-escaped string like ‘\\r\\n’ and wanted the actual non-escaped version?

Try this:

>>> ‘\\\\\\\\’.decode(’string_escape’)
‘\\\\’

I just found it today. Pretty cool, eh.