存档

文章标签 ‘python’

apache mod_python psp

2014年2月20日 评论已被关闭

之前在apache上用python都是通过cgi的方式来调用,这次尝试了一下mod_python模块。

yum安装apache,python,mod_python。
安装过程忽略。
注意python的默认版本,如果有多个版本的话,mod_python还是使用的系统的版本。
阅读全文…

分类: python 标签:

run or spawn in pexpect

2012年8月10日 评论已被关闭

Pexpect is a Python module for spawning child applications and controlling
them automatically. Pexpect can be used for automating interactive applications
such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
scripts for duplicating software package installations on different servers. It
can be used for automated software testing. Pexpect is in the spirit of Don
Libes’ Expect, but Pexpect is pure Python. Other Expect-like modules for Python
require TCL and Expect or require C extensions to be compiled. Pexpect does not
use C, Expect, or TCL extensions. It should work on any platform that supports
the standard Python pty module. The Pexpect interface focuses on ease of use so
that simple tasks are easy.

There are two main interfaces to Pexpect — the function, run() and the class,
spawn. You can call the run() function to execute a command and return the
output. This is a handy replacement for os.system().
I was failed when using spawn() to get output of remote ssh command,

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# filename: pexpect_2hei_spawn.py

import pexpect
if __name__ == ‘__main__’:

child = pexpect.spawn(‘ssh -oStrictHostKeyChecking=no myname@host.example.com’)
child.expect (‘Password:’)
child.sendline (mypassword)
child.expect (‘$’)
child.sendline (‘hostname’)
print child.before
print child.after

Function run() worked for me,that’s great!

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# filename: pexpect_2hei.py

from pexpect import *

if __name__ == ‘__main__’:

User = ‘myuser’
Host = ‘www.2hei.net’
Pwd = ‘mypwd’
print run (“ssh -oStrictHostKeyChecking=no ” +User+”@”+Host+” ‘hostname;uptime'”, events={‘(?i)password’:Pwd+’\n’})

分类: python 标签:

useful python modules zz

2011年2月12日 评论已被关闭

mark zz from http://bbs.chinaunix.net/viewthread.php?tid=1855562 :
Graphical interface wxPython http://wxpython.org   
Graphical interface pyGtk http://www.pygtk.org   
Graphical interface pyQT http://www.riverbankcomputing.co.uk/pyqt/   
Graphical interface Pmw http://pmw.sourceforge.net/   
Graphical interface Tkinter 3000 http://effbot.org/zone/wck.htm   
Graphical interface Tix http://tix.sourceforge.net/   
        
Database MySQLdb http://sourceforge.net/projects/mysql-python   
Database PyGreSQL http://www.pygresql.org/   
Database Gadfly http://gadfly.sourceforge.net/   
Database SQLAlchemy http://www.sqlalchemy.org/   
Database psycopg http://www.initd.org/pub/software/psycopg/   
Database kinterbasdb http://kinterbasdb.sourceforge.net/   
Database cx_Oracle http://www.cxtools.net/default.aspx?nav=downloads   
Database pySQLite http://initd.org/tracker/pysqlite   
        
MSN Messenger msnlib http://auriga.wearlab.de/~alb/msnlib/   
MSN Messenger pymsn http://telepathy.freedesktop.org/wiki/Pymsn   
MSN Messenger msnp http://msnp.sourceforge.net/   
Network Twisted http://twistedmatrix.com/   
Images PIL http://www.pythonware.com/products/pil/   
Images gdmodule http://newcenturycomputers.net/projects/gdmodule.html   
Images VideoCapture http://videocapture.sourceforge.net/   
        
Sciences and Maths scipy http://www.scipy.org/   
Sciences and Maths NumPy http://numpy.scipy.org//   
Sciences and Maths numarray http://www.stsci.edu/resources/software_hardware/numarray   
Sciences and Maths matplotlib http://matplotlib.sourceforge.net/   
        
Games Pygame http://www.pygame.org/news.html   
Games Pyglet http://www.pyglet.org/   
Games PySoy http://www.pysoy.org/   
Games pyOpenGL http://pyopengl.sourceforge.net/   
        
Jabber jabberpy http://jabberpy.sourceforge.net/   
        
Web scrape http://zesty.ca/python/scrape.html   
Web Beautiful Soup http://crummy.com/software/BeautifulSoup   
Web pythonweb http://www.pythonweb.org/   
Web mechanize http://wwwsearch.sourceforge.net/mechanize/   
        
Localisation geoname.py http://www.zindep.com/blog-zindep/Geoname-python/   
        
Serial port pySerial http://pyserial.sourceforge.net/   
Serial port USPP http://ibarona.googlepages.com/uspp   
        
Parallel Port pyParallel http://pyserial.sourceforge.net/pyparallel.html   
        
USB Port pyUSB http://bleyer.org/pyusb/   
        
Windows ctypes http://starship.python.net/crew/theller/ctypes/   
Windows pywin32 http://sourceforge.net/projects/pywin32/   
Windows pywinauto http://www.openqa.org/pywinauto/   
Windows pyrtf http://pyrtf.sourceforge.net/   
Windows wmi http://timgolden.me.uk/python/wmi.html   
        
PDA/GSM/Mobiles pymo http://www.awaretek.com/pymo.html   
PDA/GSM/Mobiles pyS60 http://sourceforge.net/projects/pys60   
        
Sound pySoundic http://pysonic.sourceforge.net/   
Sound pyMedia http://pymedia.org/   
Sound FMOD http://www.fmod.org/   
Sound pyMIDI http://www.cs.unc.edu/Research/assist/developer.shtml   
        
GMail libgmail http://libgmail.sourceforge.net/   
Google pyGoogle http://pygoogle.sourceforge.net/   
Expect pyExpect http://pexpect.sourceforge.net/   
WordNet pyWordNet http://osteele.com/projects/pywordnet/   
Command line cmd http://blog.doughellmann.com/2008/05/pymotw-cmd.html   
Compiler backend llvm-py http://mdevan.nfshost.com/llvm-py/   
3D VPython http://vpython.org

分类: python 标签:

compile pyhton2.7 with zlib

2011年1月14日 评论已被关闭
ensure you have installed zlib on your OS.
[2hei.net]# rpm -qa|grep zlib
zlib-1.2.3-3
zlib-devel-1.2.3-3
use source code to compile python2.7:
./configure –enable-shared && make && make install
If you still have this issue:
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file
solved:
vim /etc/ld.so.conf.d/python2.7.conf
/usr/local/lib
ldconfig
test:
[2hei.net]# python
Python 2.7.1 (r271:86832, Jan 14 2011, 01:34:20) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> imort
KeyboardInterrupt
>>> import sys
>>> sys.modules
{‘copy_reg’: <module ‘copy_reg’ from ‘/usr/local/lib/python2.7/copy_reg.pyc’>, ‘sre_compile’: <module ‘sre_compile’ from ‘/usr/local/lib/python2.7/sre_compile.pyc’>, ‘_sre’: <module ‘_sre’ (built-in)>, ‘encodings’: <module ‘encodings’ from ‘/usr/local/lib/python2.7/encodings/__init__.pyc’>, ‘site’: <module ‘site’ from ‘/usr/local/lib/python2.7/site.pyc’>, ‘__builtin__’: <module ‘__builtin__’ (built-in)>, ‘sysconfig’: <module ‘sysconfig’ from ‘/usr/local/lib/python2.7/sysconfig.pyc’>, ‘__main__’: <module ‘__main__’ (built-in)>, ‘encodings.encodings’: None, ‘abc’: <module ‘abc’ from ‘/usr/local/lib/python2.7/abc.pyc’>, ‘posixpath’: <module ‘posixpath’ from ‘/usr/local/lib/python2.7/posixpath.pyc’>, ‘_weakrefset’: <module ‘_weakrefset’ from ‘/usr/local/lib/python2.7/_weakrefset.pyc’>, ‘errno’: <module ‘errno’ (built-in)>, ‘encodings.codecs’: None, ‘sre_constants’: <module ‘sre_constants’ from ‘/usr/local/lib/python2.7/sre_constants.pyc’>, ‘re’: <module ‘re’ from ‘/usr/local/lib/python2.7/re.pyc’>, ‘_abcoll’: <module ‘_abcoll’ from ‘/usr/local/lib/python2.7/_abcoll.pyc’>, ‘types’: <module ‘types’ from ‘/usr/local/lib/python2.7/types.pyc’>, ‘_codecs’: <module ‘_codecs’ (built-in)>, ‘_warnings’: <module ‘_warnings’ (built-in)>, ‘genericpath’: <module ‘genericpath’ from ‘/usr/local/lib/python2.7/genericpath.pyc’>, ‘stat’: <module ‘stat’ from ‘/usr/local/lib/python2.7/stat.pyc’>, ‘zipimport’: <module ‘zipimport’ (built-in)>, ‘encodings.__builtin__’: None, ‘warnings’: <module ‘warnings’ from ‘/usr/local/lib/python2.7/warnings.pyc’>, ‘UserDict’: <module ‘UserDict’ from ‘/usr/local/lib/python2.7/UserDict.pyc’>, ‘encodings.utf_8’: <module ‘encodings.utf_8’ from ‘/usr/local/lib/python2.7/encodings/utf_8.pyc’>, ‘sys’: <module ‘sys’ (built-in)>, ‘codecs’: <module ‘codecs’ from ‘/usr/local/lib/python2.7/codecs.pyc’>, ‘os.path’: <module ‘posixpath’ from ‘/usr/local/lib/python2.7/posixpath.pyc’>, ‘signal’: <module ‘signal’ (built-in)>, ‘linecache’: <module ‘linecache’ from ‘/usr/local/lib/python2.7/linecache.pyc’>, ‘posix’: <module ‘posix’ (built-in)>, ‘encodings.aliases’: <module ‘encodings.aliases’ from ‘/usr/local/lib/python2.7/encodings/aliases.pyc’>, ‘exceptions’: <module ‘exceptions’ (built-in)>, ‘sre_parse’: <module ‘sre_parse’ from ‘/usr/local/lib/python2.7/sre_parse.pyc’>, ‘os’: <module ‘os’ from ‘/usr/local/lib/python2.7/os.pyc’>, ‘_weakref’: <module ‘_weakref’ (built-in)>}
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
分类: python 标签: ,

write a own tools of WorldTime with pytz

2010年6月29日 评论已被关闭

require : python2.6 pytz

#!/usr/bin/env python
# -*- coding: gbk -*-
from datetime import datetime, timedelta
from time import gmtime, strftime
from pytz import timezone
import pytz, time,os

#def convert_datetime(unix_timestamp=1143408000, tz=1, long_fmt=1):
def convert_datetime(dt=’2007-01-01 00:00:00′, tz=”, dest_fmt=”, time_stamp=0):
    fmt      = ‘%Y-%m-%d %H:%M:%S’
    if time_stamp == 0:
        dt_stamp = time.mktime(time.strptime(dt, fmt))
    else:
        dt_stamp = float(dt)
    utc      = pytz.utc
    utc_dt   = datetime.utcfromtimestamp(dt_stamp).replace(tzinfo=utc)  
    dest_tz  = timezone(tz)
    dest_dt  = dest_tz.normalize(utc_dt.astimezone(dest_tz))  
    return dest_dt.strftime(dest_fmt)

#define all citys here
citys = {‘Asia/Shanghai’:’Asia/Shanghai 上 海’,
        ‘America/Los_Angeles’:’America/Los_Angeles 旧金山’,
        ‘Etc/GMT’:’Etc/GMT 格林威治标准时间’,
        ‘US/Pacific’:’US/Pacific PT 太平洋时间’,
        ‘UTC’:’UTC 世界标准时间’,
        #’Etc/GMT+8′:’Etc/GMT+8′,
       }
 
if __name__ == ‘__main__’:
    while True:
        os.system(‘cls’)
        print ‘————–时间对照————–‘
        for k,v in citys.items():
            print convert_datetime(dt=strftime(“%Y-%m-%d %H:%M:%S”, time.localtime()), tz=k,dest_fmt=’%Y-%m-%d %H:%M:%S’),’\t[‘+v+’]’
        print ‘————————————‘
        time.sleep(1)

       
run.bat
set path=D:\python26\;%path%
D:
cd D:\Profiles\2hei.net\eclipse-project\py\myapp\src\time_format
python world_time.py

running like this:

world_time_zone.png

 

分类: python 标签:

python解析xml的字符集问题的处理

2010年5月7日 3 条评论

python版本:2.6

案例一: test.xml
<?xml version=”1.0″ encoding=”utf8″?>
调用:
xmldoc = minidom.parse(test.xml)
报错:
Traceback (most recent call last):
  File “D:\project\src\myapp\src\xml\testdomxml.py”, line 14, in <module>
    xmldoc = minidom.parse(response)
  File “D:\Python\lib\xml\dom\minidom.py”, line 1918, in parse
    return expatbuilder.parse(file)
  File “D:\Python\lib\xml\dom\expatbuilder.py”, line 928, in parse
    result = builder.parseFile(file)
  File “D:\Python\lib\xml\dom\expatbuilder.py”, line 207, in parseFile
    parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: unknown encoding: line 1, column 30

修改后:test2.xml
<?xml version=”1.0″ encoding=”utf-8″?>
再次调用
xmldoc = minidom.parse(test2.xml)
没有问题了。 囧一个!

详细可见python bug 列表: http://bugs.python.org/msg63471

案例二:
xmldoc = minidom.parse(urllib.urlopen(‘http://rss.sina.com.cn/news/marquee/ddt.xml’))
正常调用

xmldoc = minidom.parse(urllib.urlopen(‘http://news.163.com/special/00011K6L/rss_newstop.xml”))
报错:
  File “D:\Python\lib\xml\dom\expatbuilder.py”, line 207, in parseFile
    parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: unknown encoding: line 1, column 30

观察sina和163的两个rss源文件看,并未发现特别的异常,不过将163的保存为文件rssnew163.xml,在其头部添加
<?xml version=”1.0″ encoding=”utf-8″?>
然后再调用
xmldoc = minidom.parse(“rssnew163.xml”)
问题解决,看来还是字符编码的问题了。
对于使用urllib实时更新rss的就需要预先处理一下了,先保存rss文件,然后添加上述行,或者将xml文件转换成utf-8编码即可。

分类: python 标签: ,

linux下用python实现文本menu

2009年11月17日 评论已被关闭

linux用shell实现menu比较简单,今天用python实现了一个,因为python没有switch语法,所以使用了dict来替代,另外调用了linux的clear来清屏,本例只是说明一下简单的实现方法,菜单的命令或者语句可以自由发挥 呵呵。

截个图先:
2009-11-17-linux-python-menu.png

具体代码如下:
#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Author: 2hei#2hei.net
#Date: 2009-11-17 18:24

import os,sys

running = True
menu = “””
             menu
——————————
    1:   Disk info
    2:   Mem info
    3:   Network info
    4:   Sys load info
    5:   Process info
    h:   Help
    q:   Quit
——————————
“””

menu_dict={
      “h”: “echo help ^_^”,
      “1”: “df -h”,
      “2”: “free -m”,
      “3”: “netstat -lnt”,
      “4”: “uptime”,
      “5”: “ps x”
      }

def commands(args):
    cmd = menu_dict.get(args)
    return cmd

if __name__ == “__main__”:
    os.system(‘clear’)
    print menu   
    while running:
        cmd = raw_input(“Input your commond :\n”)
        if cmd != ‘q’:
            os.system(‘clear’)
            try:
                print menu
                if commands(cmd) != None:
                    fo = os.popen(commands(cmd))
                    print fo.read()
                else:
                    print “Input is Wrong!\n”
            except Exception,e:
                print menu
                print e            
        else:
            print ‘we will exit the menu\n’
            sys.exit()

分类: OpenSource, python 标签:

python 访问带有web认证的页面

2009年11月5日 评论已被关闭

python 访问带有web认证的页面,经测试方法一可用!

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#方法一
#————————————————————–
def request_auth_http(url, User = None, Pass = None):
    import urllib2
    # this creates a password manager
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()    
    passman.add_password(None, url, User, Pass)
    # create the AuthHandler
    authhandler = urllib2.HTTPBasicAuthHandler(passman)    
    opener = urllib2.build_opener(authhandler)    
    urllib2.install_opener(opener)
    pagehandle = urllib2.urlopen(url)
    data = pagehandle.read()
    pagehandle.close()
    return data

#方法二
#————————————————————–
    #auth = “Basic %s” % base64.encodestring(“%s:%s” % (User, Pass))[:-1]
    #request.add_header(“User-Agent”, “Python-2.5”)
    #request.add_header(“Authorization”,auth)
    #opener = urllib2.build_opener()    
    #data = opener.open(request).read()
    #print data
    #htmlFile = urllib2.urlopen(request)
    #htmlData = htmlFile.read()
    #htmlFile.close()
    #return htmlData

#————————————————————–
def main():
    url = “https://2hei.net/login”
    User = “user”
    Pass = “passwd”
    print request_auth_http(url,User,Pass)

#————————————————————–
if __name__ == “__main__”:
    main()

实际的结果是,方法一正解,方法二页面返回401错误。

分类: OpenSource 标签:

python use BeautifulSoup crawl weather forecast

2009年9月16日 评论已被关闭

python 使用BeautifulSoup(美汤)来抓取天气预报,简单的例子,只当练手

#!/usr/bin/env python
# -*- coding: gb2312 -*-

#get weather info from internet
#http://weather.china.com.cn/city/54511_full.html

import urllib,re,unicodedata,string,sys,re
from BeautifulSoup import *

if __name__==”__main__”:
    response = urllib.urlopen(“http://weather.china.com.cn/city/54511_full.html”)
    result = response.read()
    soup = BeautifulSoup(”.join(result))
    weather = soup.findAll(‘td’)
    list = []
    tmplist = []
    for i in xrange(0,len(weather)):
        tmpstr = weather[i].string
        if tmpstr not in [‘&nbsp;’,None,u’地方气象网站’,’合作伙伴:中国气象局中央气象台’,u’天气预报’]:
            list.append(tmpstr)
    for j in xrange(0,4):
        print list[j]

result is:
北京
气温:29 ℃~17 ℃
风向:微风
风力:小于3 级

BeautifulSoup has bug in rss tag ‘link’

2009年9月15日 1 条评论

BeautifulSoup 在处理rss页面时发现了如下问题,在rss页面中如果包含<link>标签,BeautifulSoup 处理的不好,具体如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib,re,unicodedata,string,sys,re
from BeautifulSoup import *

if __name__==”__main__”:
    response = urllib.urlopen(“http://news.163.com/special/00011K6L/rss_newstop.xml”)
    result = response.read()
    soup = BeautifulSoup(”.join(result))
    print soup.originalEncoding   
    print soup.prettify()
    news = soup.findAll(‘link’)
    print news

============
gbk
<?xml version=’1.0′ encoding=’utf-8′?>
<?xml version=’1.0′ encoding=’utf-8′?>
<rss version=”2.0″>
 <channel>
  <title>
   网易头条新闻
  </title>
  <link />
  http://news.163.com/
  <description>
   头条新闻
  </description>
  <item id=”1″>
   <title>
    <![CDATA[人民日报:人民精神文化生活”前所未有的丰富”]]>
   </title>
   <link />
   http://news.163.com/09/0914/13/5J6444CA0001124J.html
   <description>
    <![CDATA[人民网9月14日报道 新中国成立60年来,我国新闻出版事业走过了不平凡的历程。从中人们能深切感受到它对振奋精神、凝聚力量、促进改革发展稳定的巨大作用,也会切身体会到其与时代同行、与人民同心的历史性进步。 这种进步,可以从两个方面来考量。一方面是这些年来新闻出版业“前所未有的创新”。从全面推动宣传思想工作“  ]]>
    ……
   </description>
   <pubdate>
    2009-09-14 13:19:09
   </pubdate>
  </item>

[<link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />, <link />]

结果可以看到没有取到link标签的内容,应该是BeautifulSoup在自作聪明的补全标签时出现了问题。