Daemon Skeleton introduction
Daemon Skeleton introduction Past couple of days, i’ve builded some daemon tools, tired to do the same thing again and again, i think i need a template, to create python project with daemon easy and fast. So created this template Daemon Skeleton. In this template i used python-daemon as the daemonizer, and did some tweak, […]
Suppressing paramiko log
Suppressing paramiko log Backup paramiko source file. cd /usr/local/lib/python2.7/site-packages/ cp -p paramiko/util.py{,.bak} Modify paramiko/util.py. diff paramiko/util.py{,.bak} 265c265 return True Set logger name "suppress" in your code. self.client = SSHClient() self.client.set_log_channel(‘suppress’)
使用python实现简易的文件共享
使用python实现简易的文件共享 可以利用python的SimpleHTTPServer共享自己的文件给其他人 python -m SimpleHTTPServer 8000 需要从其他人那拷贝东西到自己机器上时可以用python的CGIHTTPServer这个模块,写一个CGI程序来接收文件。 # alias alias cgiserver=”ifconfig | grep –color -o ‘inet 1[79]2[^ ]*’; python -m CGIHTTPServer 8000″ # cgi_directories [‘/cgi-bin’, ‘/htbin’] # Create directory mkdir incoming/ cd incoming/ mkdir cgi-bin/ files # Create save file CGI program cat > cgi-bin/save_file.py %s cgiserver 其他人通过访问http://uripaddress:8000/upload.html来上传文件。
使用python获取mp3中的歌词
使用python获取mp3中的歌词 安装依赖库eyeD3 $ sudo port install py26-eyed3 #!/usr/bin/env python # -*- coding: utf-8 -*- “”” dump_mp3_lyris.py Created by 4Aiur on 2010-12-09. “”” import os import eyeD3 def get_mp3_file(dir): mp3_files = [] files = os.listdir(dir) for file in files: if os.path.splitext(file)[1] == ‘.mp3’: mp3_files.append(os.path.join(dir,file)) return mp3_files def dump_lyrics(mp3_file): print(‘parse %s’ % (mp3_file)) lyrics_file = ‘%s.txt’ % […]
使用python下载存放在Index中的资源
使用python下载存放在Index中的资源 Useing python threading download example. like this: wget -P result/ -nd -nH -r -l 1 -np -A pdf –http-user=book –http-password=cubook “http://www.4aiur.net/book/Programing/Python/” #!/usr/bin/env python # -*- coding: utf-8 -*- ”’ thread_download.py 下载网页中某种后缀名的文件 Created by 4Aiur on 2010-12-06. ”’ __version__ = “$Revision: 0.90 $” import threading import httplib import urlparse import base64 import re import os […]
Python 三元表达式
Python 三元表达式 In [1]: x = 100 if True else 200 In [2]: x Out[2]: 100 In [3]: x = 100 if False else 200 In [4]: x Out[4]: 200
python的ftp应用举例
python的ftp应用举例 使用python可以很方便的进行ftp操作,下面这个小程序的作用是成功下载远程服务器文件后,删除远程服务器上面的文件。 #!/usr/bin/env python2 # -*- coding: utf-8 -*- from ftplib import FTP import logging import os import socket logger = logging.getLogger(“download”) logger.setLevel(logging.DEBUG) fh = logging.FileHandler(“download.log”) formatter = logging.Formatter(“%(asctime)s – %(name)s – %(levelname)s – %(message)s”) fh.setFormatter(formatter) logger.addHandler(fh) def get_del(): ip = ‘127.0.0.1’ user_name = ‘foo’ passwd = ‘bar’ source_path = ‘/test_sour_dir/’ try: ftp = FTP(ip) […]
禁用ConfigParser配置选项转化小写
禁用ConfigParser配置选项转化小写 ConfigParser在读取配置文件后默认会把配置项目转换为小写,调用下面的方法可以关闭转换功能。 cfgparser.optionxform = str
python2.5.4 httplib timeout patch
python2.5.4 httplib timeout patch 632c632 def __init__(self, host, port=None, strict=None): 643,644d642
使用python发送带附件的邮件
使用python发送带附件的邮件 #!/usr/bin/env python # -*- coding: utf-8 -*- from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import utils, encoders import mimetypes, sys import smtplib import re import datetime def attachment(filename): fd = open(filename, “rb”) mimetype, mimeencoding = mimetypes.guess_type(filename) if mimeencoding or (mimetype is None): mimetype = “application/octet-stream” maintype, subtype […]