Displaying keyword search results 1 - 9
Created by Dr. Xi on April 16, 2014 15:40:09 Last update: April 16, 2014 15:40:09
The "with" statement makes it easy to open a file, use it then finally close it, without explicitly coding with a " try...finally " block. Example:
>>> import yaml >>> with open("app.yaml") as f:...More info: Understanding Python's "with" statement
Created by Dr. Xi on August 01, 2012 11:40:19 Last update: August 01, 2012 11:40:19
Use copyfile in shutil :
$ python Python 2.7.3 (default, Apr 20 2012, 22...
Created by Dr. Xi on June 06, 2009 18:31:44 Last update: June 25, 2012 12:37:35
You can use the system call from the os module to execute an external program:
>>> import os >>> os.system(the_command_line_st...However, the path to the executable contains a space character, the system call treats the strings after the first space as arguments, causing an error. Python doc recommends the use of the subprocess module: The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. For example, using wget to get the google home page:
>>> from subprocess import Popen, PIPE >>> (out...or
>>> import subprocess >>> subprocess.call(['cur...
Created by Dr. Xi on August 02, 2011 15:44:45 Last update: August 02, 2011 15:44:45
The time module provides functions for time manipulation:
$ python Python 2.7 (r27:82500, Sep 16 2010, 18...
Created by Dr. Xi on October 18, 2009 04:25:25 Last update: October 18, 2009 04:25:25
start python with python -v import django and print version:
Type "help", "copyright", "credits" or "license" f...
Created by Dr. Xi on June 06, 2009 19:21:57 Last update: June 06, 2009 19:21:57
import time import random # Python sleep...
Created by Dr. Xi on October 06, 2008 23:31:56 Last update: October 06, 2008 23:35:08
Use file.write instead of print .
Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12) ...
Created by Dr. Xi on October 06, 2008 22:48:08 Last update: October 06, 2008 22:50:11
A first attempt would be to create an input file like this:
userid password shell_command1 shell_...and feed the lines to the telnet client:
cat telnet_input.txt | telnet remote_host #...However, you'll learn soon enough that it doesn't work. You get output like this:
Trying 192.168.159.128... Connected to bash...What's happening? The telnet client depleted all input before the remote host had a chance to respond. Since there's no more input, the telnet client initiated to close the connection. Adding a delay between the commands makes it work:
(echo userid sleep 10 echo password ...How much time to sleep between commands is just guesswork. You can use Expect to provide more control over the automated session:
#!/usr/bin/expect # timeout script aft......
Created by Dr. Xi on October 06, 2008 18:39:53 Last update: October 06, 2008 18:39:53
Python modules are searched in the list of directories given by the variable sys.path which is initialized from: the directory containing the input script (or the current directory) the list of directories specified by the environment variable PYTHONPATH the installation-dependent default.
peaches@bashful:~/tmp$ export PYTHONPATH=`pwd` ...