随笔分类 - python
摘要:import mathclass Measurement(object): def __init__(self, val, perc): self.val = val self.perc = perc self.abs = self.val * self.perc / 100.0 def __repr__(self): return "Measurement (%r, %r)" % (self.val, self.perc) def __str__(self): return "%g+-%g%%" % (...
阅读全文
摘要:import UserDictclass FifoCache(object, UserDict.DictMixin): def __init__(self, num_entries, dct= ()): self.num_entries = num_entries self.dct = dict(dct) self.lst = [ ] def __repr__(self): return '%r (%r,%r)' % (self.__class__.__name__, self.name_entries, self.dct) ...
阅读全文
摘要:#虽然版本控制系统更强大,但这个脚本在开发工作中然后很有用。我常常对它进行定制,比如只对某个特定后缀名的文件进行备份;在for file in files 循环的内部加一个适当的测试就行了。如:name, ext = os.path.splitext(file) if ext not in ('.py', '.txt', '.doc'):continue 代码片段首先使用标准库模块os.path的splitext函数来获得文件的扩展名(以一个句号开始),放入局部变量ext中,然后,如果文件拓展名不是我们感兴趣的几个扩展名之一,我们就执行conti
阅读全文
摘要:#能够将XML文档转换为字典和列表的组合,是一个不错的主意from xml.parsers import expatclass Element(object): '''analyze a element''' def __init__(self, name, attributes): #record tag and attribute dictionary self.name = name self.attributes = attributes #clear the element cdata and its childre...
阅读全文
摘要:import xml.dom.minidomdocument = """\<slideshow><title>Demo slideshow</title><slide><title>Slide title</title><point>This is a demo</point><point>Of a program for processing slides</point></slide><slide><title&
阅读全文
摘要:#需要使用SSH协议登录本地或远程计算机,并发送命令# use ssh to control remote computersimport os, sys, paramikofrom getpass import getpassparamiko.util.log_to_file('auto_ssh.log',0)def parse_user(user,default_host, default_port): '''given name [@host[:port]],return name,host,port,if not, return default
阅读全文
摘要:需要你的主线程把处理任务托付给一个工作线程池Queue.Queue是用来协调工作线程池的最简单和最有效率的方法。因为它已经有了全局变量的保护(加锁和解锁)#-*-coding:utf-8-*-import threading, Queue, time, sys#global varialbesQin = Queue.Queue()Qout = Queue.Queue()Qerr = Queue.Queue()Pool = [ ]def report_error(): '''put error information into Qerr to report the err
阅读全文
摘要:需要将某个网络端口转发到另外一个主机(forwarding),但可能会是不同的端口(redirecting)import sys, socket, time, threadingLOGGING = Trueloglock = threading.Lock()def log(s, *a): if LOGGING: loglock.acquire() try: print '%s:%s' %(time.ctime(), (s%a)) sys.stdout.flush() finally: ...
阅读全文
摘要:import BaseHTTPServer, shutil, osfrom cStringIO import StringIOclass MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): #The http path we service and the command we service cmds = {'/ping':'ping www.thinkware.se', '/netstat': 'netstat -a', '/tracert'
阅读全文
摘要:"""Generic socket server classes. 2 3 This module tries to capture the various aspects of defining a server: 4 5 For socket-based servers: 6 7 - address family: 8 - AF_INET{,6}: IP (Internet Protocol) sockets (default) 9 - AF_UNIX: Unix domain socke...
阅读全文
摘要:#!/usr/bin/env python请参考http协议:import socketimport reimport sysdef check_webserver(address, port, resource): #build up HTTP request string if not resource.startswith('/'): resource = '/' + resource request_string = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" %(resource,address) p
阅读全文
摘要:具体文档参见:http://docs.python.org/library/xml.etree.elementtree.html例子:#-*-coding:utf-8-*-from xml.etree import ElementTree as ETfile = ET.parse('ftp.xml')ftps = file.findall('/ftp')for ftp in ftps: print ftp.attrib print ftp.get('name') print ftp.get('ip') print ftp.get(
阅读全文
摘要:#!/usr/bin/env pythonimport ftplibimport osclass FTPSync(object): def __init__(self,host,username, password, ftp_base_dir, local_base_dir, delete=False): self.host = host self.username = username self.password = password self.ftp_base_dir = f...
阅读全文
摘要:Some _mysql examplesOkay, so you want to use _mysql anyway. Here are some examples.The simplest possible database connection is:import _mysqldb=_mysql.connect()This creates a connection to the MySQL server running on the local machine using the standard UNIX socket (or named pipe on Windows), your l
阅读全文

浙公网安备 33010602011771号