Posts

Showing posts from January, 2009

Running shell cmds from python

This is done using python's subprocess module. To run a cmd and just get the return code returned, use subprocess.call(). Internally this function uses Popen(), so its arguments are the same as for the Popen constructor (see below). For example: retval = subprocess.call(args = """find . -maxdepth 2 -empty -exec rm {} \;""", shell = True, stdout = None) If you need to PIPE in/output data between caller- and sub-process, create a Popen() instance and call communicate() on it. For example, the equivalent of ps aux | grep my_daemon_name will be: # execute cmd "ps aux" p1 = Popen(["ps", "aux"], stdout=PIPE) # execute cmd "grep my_daemon_name", with cmd's stdin set to stdout of previous "ps aux" cmd p2 = Popen(["grep", "my_daemon_name"], stdin=p1.stdout, stdout=PIPE) # retrieve the stdout data from grep output = p2.communicate()[0] See also PyMOTW for more examples. class subprocess...

Howto : customize the pythonpath

To set a custom pythonpath for all users, export the environment variable in /etc/profile (debian etch). export PYTHONPATH=/data/libs/python2.4 To test if it works, login at the cmd line and issue this cmd: echo $PYTHONPATH

Howto: pretty code on Blogger

First change your template layout, to allow use of google's own code prettifier libs . Instructions are available on Luka Marinko's site . Choose the Edit Html tab when adding code. Put the code inside prettyprint tags: pre class = "prettyprint" > ... # Your code goes here / pre >

use php to call python cmd line tool

Step 1: writing the python cmd line tool Here, the optparse module does most of the heavy lifting. Note that optparse doesn't allow to set options as required (this is an oxymoron, according to the docs), so we'll handle it ourselves. #!/usr/bin/env python import optparse, sys def main(): p = optparse.OptionParser() # define options p.add_option('-u', '--user', dest = "user", help = "User account name") p.add_option('-p', '--pwd', dest = "pwd", help = "User account password") # parse cmd line string into options and arguments datamembers (raises exceptions) options, arguments = p.parse_args() # check if required args are present if options.pwd is None: print "No pwd provided." sys.exit(2) # do sth print "user = " + options.user sys.exit(0) # success if __name__ == '__main__': main() Step 2: calling the script from php # construct the cmd $cmd = "python ...

Streaming HTTP uploads with PHP ?

The problem: http classes provided by PHP packages such as PEAR and Zend framework will, when uploading a file via HTTP, read the complete file contents into memory before uploading the file. This hogs memory and can lead to errors (not enough memory available) in case of large files being transferred. PHP's curl crust seemed to be the solution, but only when you're using the PUT method. When using the POST method another issue has me stumped. Possible solution: PEAR has a new Http_Request2 class, which seems to support streaming HTTP uploads through its socket adapter. Currently in alpha.

posting a file (HTTP POST request) using curl

Curl works fine when uploading a binary file to a webservice, as long as you're using a HTTP PUT request. When you switch to HTTP POST, curl seems intent on making your life miserable. Where a PUT request allows you to freely send the file as the request body, POST coerces the file in a multipart/form-data structure. The PHP layer makes this explicit by only allowing the CURLOPT_POSTFIELDS option for indicating which file needs to be uploaded. For a PUT request, you can use: curl_setopt($ch,CURLOPT_INFILE,fopen($filepath,'rb')); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filepath)); A POST request requires you to use: curl_setopt($ch,CURLOPT_POSTFIELDS, array('file' => "@$filepath")); which will automatically pack the file content into a multipart/form-data structure. Sources: * http://w-shadow.com/blog/2007/10/08/how-to-really-upload-files-with-php/ : explains how to use CURLOPT_POSTFIELDS when POSTing a file. * http://curl.haxx.se/mail/curlphp-2...

A gentle introduction to video encoding

Write-up series by Mark Pilgrim, author of Dive Into Python .