python通过xmlrpc进行远程调用的范例演示服务器端代码# -*- coding: utf-8 -*-import SimpleXMLRPCServer#server 上面的程式碼 def Show_me_some_message(sMsg):#從遠端呼叫並且帶入參數 print "I see your call……继续阅读 » 水墨上仙 7年前 (2019-08-08) 3074浏览 2295个赞
一个Python编写的简单hangman游戏代码#!/usr/bin/env pythonimport random import cPickle class Hangman(object): '''A simple hangman game that tries to improve your voca……继续阅读 » 水墨上仙 7年前 (2019-08-03) 1495浏览 2291个赞
python执行子进程 进程间通信a.pyimport subprocess, timesubproc = subprocess.Popen(['c:\python31\python.exe', 'c:/b.py'], stdin=subprocess.PIPE, shell=True) time.sl……继续阅读 » 水墨上仙 7年前 (2019-08-03) 2372浏览 2754个赞
python rpc twisted 服务端和客户端代码演示#服务器端代码如下from twisted.web import xmlrpc, serverclass Example(xmlrpc.XMLRPC): """ An example object to be published. ……继续阅读 » 水墨上仙 7年前 (2019-08-03) 3211浏览 726个赞
wxpython自定义语法高亮控件,支持python语法高亮,只需要稍加修改就可以支持其它语言的语法高亮import keywordimport wximport wx.stc as stcimport images#--------------------------------------------------------……继续阅读 » 水墨上仙 7年前 (2019-08-03) 2613浏览 2190个赞
python集合使用范例# sets are unordered collections of unique hashable elements# Python23 tested vegaseat 09mar2005# Python v2.4 has sets built inimport setsprint "……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3465浏览 1981个赞
python读写ini配置文件import ConfigParserimport osclass ReadWriteConfFile: currentDir=os.path.dirname(__file__) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini&……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3195浏览 2173个赞
最近在做SEO的时候,为了让发的外链能够快速的收录,想到了利用ping的功能,google和百度都有相关的ping介绍,有兴趣的朋友可以去看看相关的知识。实现ping功能除了可以用一些开源的博客程序,比如WP,它是可以在后台设置ping地址的,只要设置好以后,你发帖子,就会自动的通知搜索引擎,我的博客已经更新了,而今天我用的方法是不通过WP等带有ping功能……继续阅读 » 水墨上仙 7年前 (2019-08-02) 3286浏览 1708个赞
python通过tarfile模块压缩文件夹import tarfileimport osimport sysuser = os.getenv('USERNAME')filename = '/home/%s/tmp.tgz' % userprint 'The tar file was ……继续阅读 » 水墨上仙 7年前 (2019-08-02) 2264浏览 887个赞
python 创建gtk应用程序,需要安装gtk包#!/usr/bin/env python## [代码名字: Create an Application Indicator]# [代码分类: Application Indicator]# [代码描述: How to create an application indicator and ad……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2136浏览 486个赞
我们知道python里没有switch语句,只能用多个if语句来实现,但python的字典却是万能的,下面的代码用字典实现了类似switch的功能try: sql_type = { 'STRING': 'TEXT', 'DOUBLE': 'NU……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3420浏览 302个赞
python计算代码执行时间import time start = time.time() '''code you want to time goes here''' end = time.time()elapsed = end - startprint "Time ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2176浏览 1685个赞
python在当前文件夹下设置一个简单的http服务器,执行后可以直接访问:http://127.0.0.1:8000就可以看到当前文件夹下的文件列表python -m SimpleHTTPServer……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1968浏览 574个赞
有两个数组,它们长度相同,你希望把两个数组合并成一个字典,第一个数组的作为键,第二个数组作为值,python都可以很简单的帮你实现keys = [1, 2, 3, 4]values = [55, 56, 57, 58] new_dict = dict(zip(keys, values))……继续阅读 » 水墨上仙 7年前 (2019-07-11) 4493浏览 414个赞
当你在函数定义内声明变量的时候,它们与函数外具有相同名称的其他变量没有任何关系,即变量名称对于函数来说是 局部 的。这称为变量的 作用域 。所有变量的作用域是它们被定义的块,从它们的名称被定义的那点开始。#!/usr/bin/python# Filename: func_local.pydef func(x): print 'x ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1702浏览 1379个赞
一段用来判断图片是否是色情图片的python代码,安装了pil包既可以使用转自:http://blog.csdn.net/lanphadayimport sys, Image pic = '2.jpg'img = Image.open(pic).convert('YCbCr') w, h = img.……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3077浏览 2429个赞
python链接字符串的几种方法 方法1:直接通过加号操作符相加foobar = ‘foo’ + ‘bar’ 方法2:join方法list_of_strings = ['abc', &……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2241浏览 2993个赞
python添加字符到文件代码with open("filename","a") as out: out.write("aString") ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3294浏览 1323个赞
子类化wxPython application类class MyApp(wx.App): def OnInit(self): frame = wx.Frame(parent=None, id=-1, title=”Bare”) frame.Show() return True……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2238浏览 677个赞
python中id()函数的实用研究实例>>> a = 2.5 >>> b = 2.5 >>> c = b >>> a is c 输出:False >>> a ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2548浏览 2378个赞
在编码,解码过程中,会出现某些字符始终无法处理,此时该字符出现在(‘[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]’)范围值呢,去掉这些字符即可顺利编码,import re s = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]').s……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2107浏览 641个赞
一个python高强度密码生成器,可以指定要生成的密码长度from os import urandomfrom random import choicechar_set = {'small': 'abcdefghijklmnopqrstuvwxyz', 'nums'……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3039浏览 2870个赞
下面的代码可以从网络读取一张图片,不需要保存为本地文件,直接通过Image模块对图片进行处理,这里使用到了cStringIO库,主要是把从网络读取到的图片数据模拟成本地文件。import urllib2import Imageimport cStringIOdef ImageScale(url,size): file = cStringI……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2056浏览 586个赞
用python来清除字符串里的html标签>>> print stripTags(‘Hello, world !‘)Hello, world !def stripTags(s): ''' Strips HTML tags. Taken from http://aspn……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2833浏览 1415个赞
范例:[sectionA]var1=totovar2=titihomer=simpson[sectionB]var3=kikivar4=rorojohn=doe通过下面的代码读取后,返回结果如下:In section sectionB Key john has value doe Key var3 has value kiki Key var4 has……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2944浏览 202个赞
python获取当前运行程序所在的路径import os.pathprint os.path.realpath(__file__)……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3023浏览 1736个赞
python判断当前用户是否是root,这段代码只能在unix核心的系统运行(unix,linux,mac等),不能在windows下运行import osif os.geteuid() != 0: print "This program must be run as root. Aborting." sys.ex……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2124浏览 1945个赞
这是一个python通过urllib直接登陆网站,并处理网站的session和cookieimport cookielib, urllib, urllib2login = 'ismellbacon123@yahoo.com'password = 'login'# Enable cookie support ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3444浏览 1184个赞
这段代码不用google api,而是直接抓取Google的搜索结果页,找到需要的链接后存储在links.txt, Google的搜索页面可能会变化,这段代码也需要修改import re,urllib,urllib2class GoogleHarvester: re_links = re.compile(r'<a class……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2212浏览 1966个赞
python并不提供抽象类的写法,但是如果你非要严格实现抽象类,可以使用下面的代码,实际上就是不允许用户直接调用父类的方法,如果用户调用了,则给出错误提示。class myAbstractClass: def __init__(self): if self.__class__ is myAbstractClass: ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2281浏览 439个赞
wxpython用来创建窗口应用程序,但有时候我们希望能在控制台输出一些异常或者错误提示,这段代码能够帮助你实现import sysSTDERR = sys.stderr # Keep stderr because wxPyhon will redirect it.import wx[...your wxPython program goes ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2000浏览 2816个赞
python 读取tar文件#!/usr/bin/env python# [代码名字: Open a tar file]# [代码分类: tarfile]# [代码描述: Open's a tar file and list the entries]# [代码作者: Tim Voet <tim.voet@gmail.com>……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3052浏览 1738个赞
用Python实现一个简单的算术游戏来源:http://blog.csdn.net/buaa_shang/article/details/8315829#!/usr/bin/env pythonfrom operator import add, sub from random import randint, choiceops = {'……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2409浏览 577个赞
python可以非常容易的捕获程序的输出信息,同时也可以传递给命令行程序信息#捕获程序输出信息#!/usr/bin/pythonimport subprocessmyprocess = subprocess.Popen(['net','statistics','workstation']……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2268浏览 1571个赞
我们知道python中有个range函数用来产生一个范围内的数字数组,但是浮点数没有,我们来定义一个#python中的range函数支持步进,如下:>>> print range(2,15,3)[2, 5, 8, 11, 14]#但是浮点数不支持range函数,自己定义一个类似的def floatrange(start,sto……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3065浏览 1620个赞
python编写简单抽奖系统#!/usr/bin/env python#coding=utf-8from Tkinter import *import timeimport randomclass App: def __init__(self,master): frame = Frame(master) frame.pack() ……继续阅读 » 水墨上仙 7年前 (2019-07-11) 1817浏览 1819个赞
通过python的feedparse模块可以很容易的分析出rss的内容#!/usr/bin/env python# [代码名字: Parse an RSS feed]# [代码分类: feedparser] # [代码描述: Parse and iterate over the items in an RSS feed]# [代码作者: Ti……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2866浏览 953个赞
python通过给定的url抓取远程xml文件def get_xml(self, remote_addr): remote_file = urllib.urlopen(remote_addr) remote_data = remote_file.read() remote_file.close() self.REQUEST.RESPON……继续阅读 » 水墨上仙 7年前 (2019-07-11) 2104浏览 246个赞
python判断字符串包含的方法,所有语言中,python的这种判断方法可以说是最直观简单的。#!/usr/bin/env python## [代码名字: String contains string test]# [代码分类: Python Core]# [代码描述: Test if a string contains another str……继续阅读 » 水墨上仙 7年前 (2019-07-11) 3406浏览 2120个赞
python 洋葱路由列表创建scapy 生成动态路由轨迹图scapy模板需要tcpdump程序支持,生成报表需要graphviz、ImageMagick图像处理包支持scapy(http://www.secdev.org/projects/scapy/)是一个强大的交互式数据包处理程序,它能够对数据包进行伪造或解包,包括发送数据包、包嗅探、应答和反馈……继续阅读 » 开心洋葱 8年前 (2018-10-24) 2236浏览 0评论2800个赞
本代码演示了python如何接受命令行参数##Chris Hall 9/13/11def main(argv): sys.path.append('models') #Usage will display when the user asks for help (-h) or when something ……继续阅读 » 水墨上仙 8年前 (2018-10-24) 2279浏览 1793个赞
Python 获得当前程序所在路径import osprint(os.path.abspath('.'))……继续阅读 » 水墨上仙 8年前 (2018-10-24) 2788浏览 1919个赞
py-leveldb 是 Google 的 K/V 数据库 LevelDB 的 Python 客户端开发包。import leveldbdb = leveldb.LevelDB('./db')# single putdb.Put('hello', 'world')print db.Ge……继续阅读 » 水墨上仙 8年前 (2018-10-24) 2833浏览 436个赞
python没有像C++那样的函数指针,但是python提供了更为简洁方便的函数赋值方式def foo(): print "foo" f = foo f() ……继续阅读 » 水墨上仙 8年前 (2018-10-24) 4222浏览 779个赞
Python 实现的插入法排序代码#-*- coding: utf-8 -*-# the insertation sort# 2012-07-29 noona = [3, 2, 4, 1, 5, 4, 2, 3, 6, 5, 7, 4, 1, 0]for j in range(1, len(a)): i = 0 while a[……继续阅读 » 水墨上仙 8年前 (2018-10-24) 1986浏览 1867个赞