Archive for the ‘Python’ Category

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.

PythonCard – How to Change Tab Order

Tuesday, June 2nd, 2009

There’s no tab index field, the tab traversal is simply determined by the order of the elements in the resource (*.rsrc.py) file.

http://pythoncard.sourceforge.net/framework/general_concepts_and_limitations.html

How to get a Month Name in Python

Thursday, April 16th, 2009

Here’s the shortest method I could come up with:


>>> named_month = lambda month_num:datetime.date(1900,month_num,1).strftime('%B')
>>> print named_month(5)
'May'