Python多线程——取局域网的存活IP

这里是顺序去ping 10个主机的Python程序:

  1. import os
  2. import re
  3. import time
  4. import sys
  5.  
  6. lifeline = re.compile(r"(\d) received")
  7. report = ("No response","Partial Response","Alive")
  8.  
  9. print time.ctime()
  10.  
  11. for host in range(60,70):
  12.    ip = "192.168.200."+str(host)
  13.    pingaling = os.popen("ping -q -c2 "+ip,"r")
  14.    print "Testing ",ip,
  15.    sys.stdout.flush()
  16.    while 1:
  17.       line = pingaling.readline()
  18.       if not line: break
  19.       igot = re.findall(lifeline,line)
  20.       if igot:
  21.            print report[int(igot[0])]
  22.  
  23. print time.ctime()

继续阅读 »

python多线程抓新浪天气的代码

分类:Python

  1. # -*- coding: utf-8 -*-
  2. import os,sys,time,re
  3. from threading import Thread
  4.  
  5. class DownloadWeather(Thread):
  6.     def __init__(self, path, url, num_of_workers=5, timeout = 2):
  7.         Thread.__init__(self)
  8.         self.path = path
  9.         self.url = url
  10.         #self.city = city/var/www/weather/data/' + city
  11.     url='http://weather.sina.com.cn http://php.weather.sina.com.cn/js2.php?city=' +city+ '&time=' +wtime
  12. <a href="http://www.linuxany.com/archives/298.html#more-298" class="more-link">继续阅读 &raquo;</a>

python通过表单的post方法自动用户登陆

分类:Python

  1. def post3():
  2. # for mail.sina.com.cn
  3.     import urllib,urllib2,cookielib
  4.     cj = cookielib.CookieJar()
  5.     url_login = 'http://mail.sina.com.cn/cgi-bin/login.cgi'
  6.     body = (('logintype','login'), ('u','*****'), ('psw', '******'))
  7.     opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  8.     opener.addheaders = [('User-agent', 'Opera/9.23')]
  9.     urllib2.install_opener(opener)
  10.     req=urllib2.Request(url_login,urllib.urlencode(body))
  11.     u=urllib2.urlopen(req)
  12.     print u.read().decode('utf-8').encode('gbk')

Python的标准logging模块

分类:Python

Python Standard Logging

Python的标准logging模块

Python 2.3 introduced the logging module to the Python standard library. logging provides a standard interface for outputting information from a running application. The classic example of a logging mechanism is writing data to a text file, which is a plain old log file. While the log file is likely the most common form of logging, the logging module provides the ability to send information to file-like objects, TCP and UDP sockets, email servers, the Unix syslog, the NT event log, memory buffers, and HTTP servers in addition to “real” files.

从Python2.3起,Python的标准库加入了logging模块.logging模块给运行中的应用提供了一个标准的信息输出接口.典型的logging机制实现是把要输出的数据简单地写到一个txt文件中去.写log文件的方式是一种常见的打log的方式,而logging模块提供的更多,它可以把输出信息输出到所有类文件的对象中去,甚至TCP和UDP的sockets,email服务器,Unix的syslog系统,NT系列的事件log系统,内存的buffer和HTTP服务器,当然还有”真正的”文件中去. 继续阅读 »

[mod_python] apache log handler

分类:Python

# A log handler for mod-python

  1. from mod_python import apache
  2. import logging
  3.  
  4. class ProxyLogThing:
  5.     """A proxy for default Apache logging."""
  6.  
  7.     def __init__(self):
  8.         # No need to do anything.
  9.  
  10.     def log_error(msg, lvl):
  11.         apache.log_error(msg, lvl)

继续阅读 »

python中日志Logging模块的简单使用

分类:Python

  1. def initlog():
  2.     import logging
  3.     logger = logging.getLogger()
  4.     hdlr = logging.FileHandler(logfile)
  5.     formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
  6.     hdlr.setFormatter(formatter)
  7.     logger.addHandler(hdlr)
  8.     logger.setLevel(logging.NOTSET)
  9.     return logger

继续阅读 »

Django配置

分类:Python

配置mod_python

1.使用apache_2.0.54-win32-x86-no_ssl.msi
http://archive.apache.org/dist/httpd/binaries/win32/

2.安装mod_python-3.2.8.win32-py2.4.exe
http://apache.justdn.org/httpd/modpython/

mod_python中文文档
http://man.chinaunix.net/develop/python/mod_python/mod_python.h

继续阅读 »


返回顶部