将这段代码放入你的脚本中,类似:urllib.urlretrieve(getFile, saveFile, reporthook=report)第三个参数如下面的函数定义report,urlretrieve下载文件时会实时回调report函数,显示下载进度def report(count, blockSize, totalSize): perc……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2465浏览 1590个赞
python-memcache 基本使用方法代码,如果没有安装python-memcached可以先通过easy_install安装:easy_install python-memcached#!/usr/bin/env python import memcache mc = memcache.Client(['127.0.0.1:……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3321浏览 1651个赞
python链接远程ftp服务器并列出目录下的文件,这段python代码用到了pysftp模块,使用sftp协议,对数据进行加密传输import pysftp srv = pysftp.Connection(host="your_FTP_server", username="your_username",p……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1710浏览 1646个赞
python通过BeautifulSoup分析网页信息,这段python代码查找网页上的所有链接,分析所有的span标签,并查找class包含titletext的span的内容#import the library used to query a websiteimport urllib2 #specify the url you want to……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2129浏览 2826个赞
给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好。import randomn = random.randint(1, 99)guess = int(raw_input("Enter an integer from 1 to 99: "))……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2228浏览 2223个赞
os是python用于操作系统级的操作模块,包括获取系统信息,文件及目录操作等import osos.system() #用于执行系统命令os.environ() #返回用户环境信息os.getcwd() #返回当前工作目录os.getgid() #返回当先进程的group idos.getuid() #返回当前进程的用户id……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3119浏览 2415个赞
python统计字符串中指定字符出现的次数,例如想统计字符串中空格的数量s = "Count, the number of spaces."print s.count(" ") x = "I like to program in Python"print x.count(&qu……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1783浏览 226个赞
下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码 python中的string对象有几个常用的方法用来输出各种不同的字符:string.ascii_letters输出ascii码的所有字符string.digits输出 ’……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2192浏览 617个赞
下面的python代码展示python中字典的常用操作,字典在python开发中有着举足轻重的地位,掌握字典操作相当重要#创建一空字典x = {}#创建包含三个项目的字典x = {"one":1, "two":2, "three":3}#访问其中的一个元素x['two……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2067浏览 546个赞
python实现的telnet客户端程序,python自带一个telnetlib模块,可以通过其Telnet类实现telnet操作import getpassimport sysimport telnetlib HOST = "hostname" user = raw_input("Enter your rem……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2161浏览 456个赞
给定网址,通过这段python代码可以很容易获取域名信息import urlparseurl = "http://www.75271.com"domain = urlparse.urlsplit(url)[1].split(':')[0]print "The domain name of the……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2764浏览 2167个赞
这段python代码通过urllib2抓取网页,然后通过简单的正则表达式分析网页上的全部url地址import urllib2import re #connect to a URLwebsite = urllib2.urlopen(url) #read html codehtml = website.read() #use re.fi……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3100浏览 1040个赞
python通过calendar输出指定年份的全年日历import calendar print "Show a given years monthly calendar" print '' year = int(raw_input("Enter the year")) pri……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2155浏览 727个赞
下面python代码通过urllib2抓取指定的url的内容,并且使用自定义的user-agent,防止网站屏蔽采集器import urllib2 req = urllib2.Request('http://192.168.1.2/') req.add_header('User-agent', '……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3309浏览 865个赞
下面的python代码可用于检测字符串,包括是否全部为数字,是否包含数字,是否包含标题单词,是否包含大写字母,是否包含小写字母,是否包含空格,是否以指定的字符开头和结尾。my_string = "Hello World"my_string.isalnum() #检测所有字符是否都是数字my_string.isalpha() ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1599浏览 600个赞
python中列表元素连接方法join的用法 创建列表>>> music = ["Abba","Rolling Stones","Black Sabbath","Metallica"……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3284浏览 818个赞
python常用列表(数组)操作演示,包括创建列表,添加新元素,获取列表长度,删除元素,数组翻转,列表排序,在指定位置添加元素,统计某元素出现的次数等等s = ['h','e','l','l','o'] #create a lists.append(&……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1596浏览 585个赞
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.……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2677浏览 2535个赞
python开发的简单的猜单词游戏代码,这是一个简单的人机交互的小游戏,用户有10次机会来猜对单词,如果用户猜对了其中的一个字母,系统会在正确的位置显示该字母。#importing the time moduleimport time #welcoming the username = raw_input("What is your ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1740浏览 2012个赞
python根据用户输入从电影网站获取影片信息的范例代码,这段python代码主要演示了用户终端输入,正则表达式,网页抓取等#!/usr/bin/env python27 #Importing the modules from BeautifulSoup import BeautifulSoupimport sysimport urllib……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3411浏览 2505个赞
通过这段代码可以一目了然的知道scrapy的抓取页面结构,调用也非常简单# This is a script to print the crawl tree of spider run.# # 使用方法:# # $ python ctree.py myspider.log# None# http://www.e……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2022浏览 1014个赞
scrapy自定义pipeline类将采集数据保存到mongodb# Standard Python library imports # 3rd party modulesimport pymongo from scrapy import logfrom scrapy.conf import settingsfrom scrapy.exc……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2386浏览 1039个赞
通过scrapy实现的一个简单的蜘蛛采集程序# Standard Python library imports # 3rd party importsfrom scrapy.contrib.spiders import CrawlSpider, Rulefrom scrapy.contrib.linkextractors.sgml import……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2238浏览 763个赞
一个简单的python编写的猜数字游戏代码import randomguesses_made = 0name = raw_input('Hello! What is your name?\n')number = random.randint(1, 20)print 'Well, {0}, I am think……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3187浏览 1000个赞
Queue模块允许创建指定长度的队列. 下面是Queue模块的常用方法:get():删除并返回队列中的一个项目put(): 添加项目到队列qsize() : 返回队列中元素的个数empty(): 队列为空返回True否则返回Falsefull(): 队列已满返回True,负责返回False#!/usr/bin/pythonimport Q……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2114浏览 573个赞
下面的python代码演示线程锁的用法和线程同步#!/usr/bin/pythonimport threadingimport timeclass myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Th……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3217浏览 319个赞
python多线程模块threading使用范例代码#!/usr/bin/pythonimport threadingimport timeexitFlag = 0class myThread (threading.Thread): def __init__(self, threadID, name, counter): ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2661浏览 1566个赞
python多线程模块thread使用范例#!/usr/bin/pythonimport threadimport time# Define a function for the threaddef print_time( threadName, delay): count = 0 while count < 5: ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2663浏览 2376个赞
python实现的简单的socket通信的客户端和服务器端 服务端代码#!/usr/bin/python # This is server.py fileimport socket # Import socket module……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3212浏览 2901个赞
实际上返回的是计算机的cpu核心数,比如cpu是双核的,则返回2,如果双四核cpu,则返回8from multiprocessing import cpu_countprint(cpu_count()) 本机是四核电脑,返回结果:4……继续阅读 » 水墨上仙 5年前 (2021-03-22) 4428浏览 1047个赞
将Queue作为中间通道进行数据传递,Queue是线程和进程安全的from multiprocessing import Process, Queuedef f(q): q.put([42, None, 'hello'])if __name__ == '__main__': q = Que……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2451浏览 2038个赞
下面的python代码根据进程的名字调用windows的taskkill命令杀死指定的进程import os command = 'taskkill /F /IM QQ.exe' #比如杀死QQ进程os.system(command) ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2631浏览 2045个赞
这个python代码创建了多个process子进程,创建完成后先start(),最后统一join,这样所有子进程会并行执行。from multiprocessing import Processimport sys, osimport timedef timetask(times): time.sleep(times) prin……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2093浏览 2512个赞
这段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: ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3134浏览 2203个赞
multipressing模块式python 2.6版本加入的,通过这个模块可以轻松实现异步调用from multiprocessing import Pooldef f(x): return x*xif __name__ == '__main__': pool = Pool(processes=1) ……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2957浏览 1726个赞
很多时候函数内部包含了一些不可预知的事情,比如调用其它软件,从网络抓取信息,可能某个函数会卡在某个地方不动态,这段代码可以用来限制函数的执行时间,只需要在函数的上方添加一个装饰器,timelimited(2)就可以限定函数必须在2秒内执行完成,如果执行完成则返回函数正常的返回值,如果执行超时则会抛出错误信息。代码参考:http://augustwu.ite……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2982浏览 2504个赞
这段代码通过shell调用7z对apk包进行解压缩def run_shell(command, mayFreeze=False): def check_retcode(retcode, cmd): if 0 != retcode: print >> sys.stderr, 'err executing ' +……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3483浏览 1129个赞
这段代码通过MySQLdb模块连接mysql数据库,然后查询employee表中income字段大于1000的数据输出#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuse……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2792浏览 148个赞
下面的python代码通过MySQLdb连接数据库,然后通过sql语句添加数据,添加数据使用参数化查询方式#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser"……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2014浏览 2328个赞
下面的python代码通过MySQLdb代码连接mysql数据库,通过sql语句添加数据,并演示了如何通过commit提交数据和rollback回滚操作#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost&qu……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1932浏览 1013个赞
下面的python代码通过MySQLdb模块连接数据库,然后通过Sql语句在数据库中创建表,最后关闭数据库#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser"……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3013浏览 2512个赞
下面的python代码通过MySQLdb模块链接mysql数据库,然后打开数据库,并通过sql语句查询mysql的版本号,最后关闭数据库连接#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost",&q……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3384浏览 1691个赞
python实现的端口转发器,支持udp端口转发由于工作需要用到一个端口转发器,并且要求支持TCP和UDP协议。在网上找了蛮久,但没有中意的。于是就自己写了一个。这个转发器是基于python cookbook的一个示例改写的,原先的这个示例只支持TCP协议,我这里增加了UDP协议的支持,程序写的不怎么好,不过它确实能用,哈哈哈!por……继续阅读 » 水墨上仙 5年前 (2021-03-22) 2270浏览 1328个赞
一个简单的python开发的监控程序,当指定网页状态不正常是通过smtp发送通知邮件转自:http://www.lastme.com/#!/usr/bin/env python# -*- coding: UTF-8 -*-#author libertyspy#link http://www.lastme.comimport sock……继续阅读 » 水墨上仙 5年前 (2021-03-22) 3273浏览 2018个赞
python检测RabbitMQ的状态是否正常import socket def check_aliveness(ip, port): sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(1) try: sk.connect……继续阅读 » 水墨上仙 5年前 (2021-03-22) 1857浏览 2786个赞