一个python自定义的加密解密函数,下面的代码实现了对字符串的加密盒解密,调用简单def encrypt(key, s): b = bytearray(str(s).encode("gbk")) n = len(b) # 求出 b 的字节数 c = bytearray(n*2) j = 0 ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2182浏览 380个赞
python通过zlib压缩和解压缩字符串,可以自定义压缩级别,级别越高压缩的约厉害,效率要低一些import zlibcompressedString = zlib.compress(originalString, 9) # 采用9级压缩decompressedString = zlib.decompress(compressedStrin……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2835浏览 1153个赞
python自定义isnumber函数判断字符串是否为数字''' isnumeric.pytest a numeric string s if it's usable for int(s) or float(s)'''def isnumeric(s): '&……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1546浏览 2763个赞
python不定义函数实现数字增减,原来python还可以这么用,你一定想不到inc = (1).__add__print(inc(4))#5sub = (-1).__radd__print(sub(5))#4……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3035浏览 1870个赞
python通过Tkinter显示网络图片''' tk_image_view_url_io.pydisplay an image from a URL using Tkinter, PIL and data_streamtested with Python27 and Python33 by vegaseat 0……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3092浏览 697个赞
python通过PyGame绘制图像并保存为图片文件''' pg_draw_circle_save101.pydraw a blue solid circle on a white backgroundsave the drawing to an image filefor result see http://prn……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1741浏览 1893个赞
将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report)第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度def report(count, blockSize, totalSize): perc……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1950浏览 1176个赞
python-memcache 基本使用方法代码,如果没有安装python-memcached可以先通过easy_install安装:easy_install python-memcached#!/usr/bin/env python import memcache mc = memcache.Client(['127.0.0.1:……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2912浏览 598个赞
python链接远程ftp服务器并列出目录下的文件,这段python代码用到了pysftp模块,使用sftp协议,对数据进行加密传输import pysftp srv = pysftp.Connection(host="your_FTP_server", username="your_username",p……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3112浏览 2696个赞
python通过BeautifulSoup分析网页信息,这段python代码查找网页上的所有链接,分析所有的span标签,并查找class包含titletext的span的内容#import the library used to query a websiteimport urllib2 #specify the url you want to……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3085浏览 193个赞
给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好。import randomn = random.randint(1, 99)guess = int(raw_input("Enter an integer from 1 to 99: "))……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3005浏览 2171个赞
os是python用于操作系统级的操作模块,包括获取系统信息,文件及目录操作等import osos.system() #用于执行系统命令os.environ() #返回用户环境信息os.getcwd() #返回当前工作目录os.getgid() #返回当先进程的group idos.getuid() #返回当前进程的用户id……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2151浏览 1842个赞
python统计字符串中指定字符出现的次数,例如想统计字符串中空格的数量s = "Count, the number of spaces."print s.count(" ") x = "I like to program in Python"print x.count(&qu……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2866浏览 1012个赞
下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码 python中的string对象有几个常用的方法用来输出各种不同的字符:string.ascii_letters输出ascii码的所有字符string.digits输出 ’……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1169浏览 2227个赞
下面的python代码展示python中字典的常用操作,字典在python开发中有着举足轻重的地位,掌握字典操作相当重要#创建一空字典x = {}#创建包含三个项目的字典x = {"one":1, "two":2, "three":3}#访问其中的一个元素x['two……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2315浏览 1269个赞
python实现的telnet客户端程序,python自带一个telnetlib模块,可以通过其Telnet类实现telnet操作import getpassimport sysimport telnetlib HOST = "hostname" user = raw_input("Enter your rem……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1714浏览 2018个赞
给定网址,通过这段python代码可以很容易获取域名信息import urlparseurl = "http://www.75271.com"domain = urlparse.urlsplit(url)[1].split(':')[0]print "The domain name of the……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2884浏览 1821个赞
这段python代码通过urllib2抓取网页,然后通过简单的正则表达式分析网页上的全部url地址import urllib2import re #connect to a URLwebsite = urllib2.urlopen(url) #read html codehtml = website.read() #use re.fi……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1540浏览 1242个赞
python通过calendar输出指定年份的全年日历import calendar print "Show a given years monthly calendar" print '' year = int(raw_input("Enter the year")) pri……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2084浏览 1175个赞
下面python代码通过urllib2抓取指定的url的内容,并且使用自定义的user-agent,防止网站屏蔽采集器import urllib2 req = urllib2.Request('http://192.168.1.2/') req.add_header('User-agent', '……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1665浏览 1278个赞
下面的python代码可用于检测字符串,包括是否全部为数字,是否包含数字,是否包含标题单词,是否包含大写字母,是否包含小写字母,是否包含空格,是否以指定的字符开头和结尾。my_string = "Hello World"my_string.isalnum() #检测所有字符是否都是数字my_string.isalpha() ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2164浏览 1647个赞
python中列表元素连接方法join的用法 创建列表>>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1882浏览 1851个赞
python常用列表(数组)操作演示,包括创建列表,添加新元素,获取列表长度,删除元素,数组翻转,列表排序,在指定位置添加元素,统计某元素出现的次数等等s = ['h','e','l','l','o'] #create a lists.append(&……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1745浏览 2466个赞
python从显示ip地址的网站获取本机外网ip,这段python代码抓取网站上的ip地址信息import urllibimport re print "we will try to open this url, in order to get IP Address" url = "http://checkip.……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2156浏览 1594个赞
python开发的简单的猜单词游戏代码,这是一个简单的人机交互的小游戏,用户有10次机会来猜对单词,如果用户猜对了其中的一个字母,系统会在正确的位置显示该字母。#importing the time moduleimport time #welcoming the username = raw_input("What is your ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3126浏览 1725个赞
python根据用户输入从电影网站获取影片信息的范例代码,这段python代码主要演示了用户终端输入,正则表达式,网页抓取等#!/usr/bin/env python27 #Importing the modules from BeautifulSoup import BeautifulSoupimport sysimport urllib……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1480浏览 1592个赞
通过这段代码可以一目了然的知道scrapy的抓取页面结构,调用也非常简单# This is a script to print the crawl tree of spider run.# # 使用方法:# # $ python ctree.py myspider.log# None# http://www.e……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2013浏览 2662个赞
scrapy自定义pipeline类将采集数据保存到mongodb# Standard Python library imports # 3rd party modulesimport pymongo from scrapy import logfrom scrapy.conf import settingsfrom scrapy.exc……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2514浏览 1577个赞
通过scrapy实现的一个简单的蜘蛛采集程序# Standard Python library imports # 3rd party importsfrom scrapy.contrib.spiders import CrawlSpider, Rulefrom scrapy.contrib.linkextractors.sgml import……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1972浏览 712个赞
一个简单的python编写的猜数字游戏代码import randomguesses_made = 0name = raw_input('Hello! What is your name?\n')number = random.randint(1, 20)print 'Well, {0}, I am think……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2188浏览 1092个赞
Queue模块允许创建指定长度的队列. 下面是Queue模块的常用方法:get():删除并返回队列中的一个项目put(): 添加项目到队列qsize() : 返回队列中元素的个数empty(): 队列为空返回True否则返回Falsefull(): 队列已满返回True,负责返回False#!/usr/bin/pythonimport Q……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1350浏览 2781个赞
下面的python代码演示线程锁的用法和线程同步#!/usr/bin/pythonimport threadingimport timeclass myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Th……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2796浏览 1905个赞
python多线程模块threading使用范例代码#!/usr/bin/pythonimport threadingimport timeexitFlag = 0class myThread (threading.Thread): def __init__(self, threadID, name, counter): ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2705浏览 272个赞
python多线程模块thread使用范例#!/usr/bin/pythonimport threadimport time# Define a function for the threaddef print_time( threadName, delay): count = 0 while count < 5: ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1558浏览 2323个赞
python实现的简单的socket通信的客户端和服务器端 服务端代码#!/usr/bin/python # This is server.py fileimport socket # Import socket module……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2658浏览 2869个赞
实际上返回的是计算机的cpu核心数,比如cpu是双核的,则返回2,如果双四核cpu,则返回8from multiprocessing import cpu_countprint(cpu_count()) 本机是四核电脑,返回结果:4……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2816浏览 2784个赞
将Queue作为中间通道进行数据传递,Queue是线程和进程安全的from multiprocessing import Process, Queuedef f(q): q.put([42, None, 'hello'])if __name__ == '__main__': q = Que……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1629浏览 1065个赞
下面的python代码根据进程的名字调用windows的taskkill命令杀死指定的进程import os command = 'taskkill /F /IM QQ.exe' #比如杀死QQ进程os.system(command) ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 3457浏览 2595个赞
这个python代码创建了多个process子进程,创建完成后先start(),最后统一join,这样所有子进程会并行执行。from multiprocessing import Processimport sys, osimport timedef timetask(times): time.sleep(times) prin……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2040浏览 724个赞
这段python代码对c:\temp目录下的所有的文件名的”scroll_1”替换为”scroll_00”import ospath = 'c:\\temp'for file in os.listdir(path): if os.path.isfile(os.path.join(path,file))==True: ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2754浏览 435个赞
multipressing模块式python 2.6版本加入的,通过这个模块可以轻松实现异步调用from multiprocessing import Pooldef f(x): return x*xif __name__ == '__main__': pool = Pool(processes=1) ……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1336浏览 1872个赞
很多时候函数内部包含了一些不可预知的事情,比如调用其它软件,从网络抓取信息,可能某个函数会卡在某个地方不动态,这段代码可以用来限制函数的执行时间,只需要在函数的上方添加一个装饰器,timelimited(2)就可以限定函数必须在2秒内执行完成,如果执行完成则返回函数正常的返回值,如果执行超时则会抛出错误信息。代码参考:http://augustwu.ite……继续阅读 » 水墨上仙 4年前 (2021-03-22) 1562浏览 2579个赞
这段代码通过shell调用7z对apk包进行解压缩def run_shell(command, mayFreeze=False): def check_retcode(retcode, cmd): if 0 != retcode: print >> sys.stderr, 'err executing ' +……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2044浏览 2903个赞
这段代码通过MySQLdb模块连接mysql数据库,然后查询employee表中income字段大于1000的数据输出#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuse……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2071浏览 1873个赞
下面的python代码通过MySQLdb连接数据库,然后通过sql语句添加数据,添加数据使用参数化查询方式#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser"……继续阅读 » 水墨上仙 4年前 (2021-03-22) 2026浏览 778个赞