2011年7月26日 星期二

python getopt



import getopt, sys

def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-o", "--output"):
output = a
else:
assert False, "unhandled option"
# ...

if __name__ == "__main__":
main()


"ho:v" 允許-h -o -v, 其中-o需要參數(後面多:)
["help", "output="] 允許--help --output, 其中--output需要參數(後面多=)

http://docs.python.org/library/getopt.html

python subprocess


#!/usr/bin/python
from subprocess import *
from shlex import *

command = "ls -l"
print command
output = Popen(split(command), stdout=PIPE, stderr=PIPE)
outputData = output.communicate()[0]
returncode = output.returncode





http://docs.python.org/library/subprocess.html

2011年7月13日 星期三

Disable Apache folder listing for each folder

1. enable .htaccess file override
edit file /etc/httpd/conf/httpd.conf
search "htaccess", changed below setting to
AllowOverride All

2. go to folder that will disable folder listing
create file .htaccess
add this content:
IndexIgnore *

3. Restart Apache
service httpd restart

Reference:
http://www.cyberciti.biz/faq/apache-web-server-prevent-directory-folder-listing/
http://www.cyberciti.biz/faq/apache-htaccess/

2011年7月4日 星期一

Manipulating Strings

bash下字串的操作

chars="12345678"
echo ${chars:2:3} #234
format: ${string:position:length}
position index從1開始
echo ${chars:2} #345678
從0開始算

reference:
http://tldp.org/LDP/abs/html/string-manipulation.html