Recently in python Category

gg resolve from bbs:
vi /usr/lib/python2.6/site-packages/pxssh.py
#add line 134,135:
    123     def synch_original_prompt (self):
    124
    125         """This attempts to find the prompt. Basically, press enter and record
    126         the response; press enter again and record the response; if the two
    127         responses are similar then assume we are at the original prompt. """
    128
    129         # All of these timing pace values are magic.
    130         # I came up with these based on what seemed reliable for
    131         # connecting to a heavily loaded machine I have.
    132         # If latency is worse than these values then this will fail.
    133
    134         self.sendline()
    135         time.sleep(0.5)

    136         self.read_nonblocking(size=10000,timeout=1) # GAS: Clear out the cache before getting the prompt
    137         time.sleep(0.1)

we just put something there before ssh expect read.
| | Comments (0) | TrackBacks (0)
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
| | Comments (0) | TrackBacks (0)
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
| | Comments (0) | TrackBacks (0)
when meet This field is required at django forms, we can add "required=False" at Fields.

from django import forms

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    cc_myself = forms.BooleanField(required=False)

| | Comments (0) | TrackBacks (0)
Scripts and executables must do two things (at a minimum) in order to function as Nagios plugins:
1.Exit with one of several possible return values
2.Return at least one line of text output to STDOUT

Plugin Return Code Service State Host State
0 OK UP
1 WARNING UP or DOWN/UNREACHABLE*
2 CRITICAL DOWN/UNREACHABLE
3 UNKNOWN DOWN/UNREACHABLE
Note: If the use_aggressive_host_checking option is enabled, return codes of 1 will result in a host
state of DOWN or UNREACHABLE. Otherwise return codes of 1 will result in a host state of UP.

Plugin Output Spec
At a minimum, plugins should return at least one of text output. Beginning with Nagios 3, plugins can
optionally return multiple lines of output. Plugins may also return optional performance data that can
be processed by external applications. The basic format for plugin output is shown below:
TEXT OUTPUT | OPTIONAL PERFDATA
LONG TEXT LINE 1
LONG TEXT LINE 2
...
LONG TEXT LINE N | PERFDATA LINE 2
PERFDATA LINE 3
...
PERFDATA LINE N

this is my python scripts:
#!/usr/bin/evn python
# -*- coding: utf-8 -*-

import sys,getopt
import memcache

memcached_host='2hei.net'
memcached_port=11211
Warning_item=120
Critical_item=20

def usage():
    print """
Usage: check_memcached [-h|--help] [-w|--warning curr_items] [-c|--critical curr_items]"
Warning curr_items defaults to 120
Critical curr_items defaults to 20
"""
    sys.exit(3)

#get curr_items from memcache stats
def get_memcache_curr_items(mc):
    #mc = memcache.Client([memcached_host+':'+str(memcached_port)], debug=0)
    stats = mc.get_stats()[0][1]   
    #for i in xrange(0,100):
    #    mc.set('key'+str(i),'value'+str(i))
    #for k,v in stats.items():
    #    print k,v
    items = stats.get('curr_items')
    return items

if __name__ == "__main__":
    warning_item = 0
    critical_item = 0

    try:
        options, args = getopt.getopt(sys.argv[1:],"h:w:c:","--help --warning= --critical=",)
    except getopt.GetoptError:
        usage()
        sys.exit(3)

    try:
        mc = memcache.Client([memcached_host+':'+str(memcached_port)], debug=0)
        items = get_memcache_curr_items(mc)
        mc.disconnect_all()
    except Exception:
        print "Cannot get memcache's curr_items.",Exception
        sys.exit(3)

    for name, value in options:
        if name in ("-h", "--help"):
            usage()
            sys.exit(3)
        if name in ("-w", "--warning"):
            warning_item = value
        if name in ("-c", "--critical"):
            critical_item = value

    if warning_item == 0:
        warning_item = Warning_item
    if critical_item == 0:
        critical_item = Critical_item

    if int(items) <= int(critical_item):
        print 'MEMCACHED_ITEM CRITICAL: curr_items is:',items
        sys.exit(2)
    if int(items) <= int(warning_item):
        print 'MEMCACHED_ITEM WARNING: curr_items is:',items
        sys.exit(1)
    else:
        print 'MEMCACHED_ITEM OK: curr_items is:',items
        sys.exit(0)

when encounter errors:
CHECK_NRPE: No output returned from daemon.
or
CHECK_NRPE: Received 0 bytes from daemon.  Check the remote server logs for error messages.
this shows your plugins return output is null

| | Comments (1) | TrackBacks (0)
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













 

| | Comments (0) | TrackBacks (0)

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编码即可。

| | Comments (3) | TrackBacks (0)
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()


| | Comments (0) | TrackBacks (0)

xen的其中一个虚拟机无法使用,重启xen后发现无法启动。

最后发现原因是升级了python版本所致,升级到了2.6.2:

之前的版本是:python -V
2.4.3

降回到原来的python版本,重试OK!

| | Comments (0) | TrackBacks (0)
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 级
| | Comments (0) | TrackBacks (0)
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在自作聪明的补全标签时出现了问题。

| | Comments (1) | TrackBacks (0)

在使用python写的cgi是遇到如下问题
Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 20] Not a directory: '/dev/null/.python-eggs' The Python egg cache directory is currently set to: /dev/null/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

分析其主要原因是启动http服务的用户没有主目录,所以系统默认给分配了/dev/null这个目录,所以当要在/dev/null/中解开egg文件时,会有错误
解决办法为设定PYTHON_EGG_CACHE变量即可,具体如下:

import os
os.environ['PYTHON_EGG_CACHE'] = '/tmp/'

 

| | Comments (0) | TrackBacks (0)

关于本篇存档

本页是归档于分类python

上一分类:php

下一分类:resin

回到首页 或者查看归档文章

  • Powered by FeedBurner
  • Add to Google Reader or Homepage
  • Add to My AOL
  • Subscribe in NewsGator Online
  • del.icio.us/2heidel.icio.us/2hei
  • Subscribe to feed feeds