Recent Notes

Displaying keyword search results 1 - 6
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 alfa on June 07, 2011 11:34:26    Last update: June 07, 2011 11:36:37
This is an example that uses dynamic proxies to trace method calls (in logging) and print out elapsed times for them. Because dynamic proxies can only be generated for interfaces, the service classes must be implemented with interface-implementation pairs. Create services A and B. A.java : public interface A { public void service1()... AImpl.java : import java.util.Random; public class AImpl... B.java : public interface B { public void service1()... BImpl.java : public class BImpl implements B { public vo... The call trace proxy: import java.lang.reflect.*; class TraceProx... The performance proxy: import java.lang.reflect.*; class Performan... The service factory: import java.lang.reflect.*; public class Se... The test class: public class Test { public static void main... The output: Entering AImpl.service1 Entering BImpl.service1... The above example has no information...
Created by Dr. Xi on March 02, 2011 11:39:18    Last update: March 09, 2011 12:19:30
Some peculiarities about Java PrintWriter: PrintWriter never throws any exceptions. From JavaDoc : Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError(). When error occurs, you'll never know anything more than that it occured, because checkError returns boolean. When a character is out of the range of the character encoding of the PrintWriter, it prints a question mark (?). But this is not an error. Test code: import java.io.*; public class TestPrintWri... Latin1 test result: java TestPrintWriter iso-8859-1 | od -bc 000000... UTF-8 test result: java TestPrintWriter utf-8 | od -bc 0000000 141... Also, the constructor throws a FileNotFoundException when you try to write to a...
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 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......