python实现线程池原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码出处:http://blog.csdn.net/liujian0616/article/details/7951081import threadingimport timeimport signalimport oscla……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2951浏览 746个赞
用python实现windows服务-在服务中新建进程出处:http://blog.csdn.net/liujian0616/article/details/7950758 需要安装的软件:python和pywin32,我这里装的分别是python-2.6.amd64、pywin32-……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3258浏览 1852个赞
python xmlrpc实现二进制文件传输的代码 服务器端from SimpleXMLRPCServer import SimpleXMLRPCServerimport xmlrpclib def python_logo(): handle = open(&quo……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2563浏览 290个赞
python urllib.urlencode用法演示>>> import urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f =……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1823浏览 1219个赞
python写的FTP简单上传下载文件出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/983638#!/usr/bin/env python # -*- coding: utf-8 -*- from ftplib import FTP def ftp_up(filenam……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2458浏览 1839个赞
通过python自动连接ssh服务器#!/usr/bin/python#-*- coding:utf-8 -*-import sys, time, ostry: import pexpectexcept ImportError: print """ You must install ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2416浏览 1342个赞
python判断文件和文件夹是否存在import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录不存在就返回False……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2514浏览 1696个赞
python通过urllib2提交http post请求#!/usr/bin/python #coding=utf-8 import urllib import urllib2 def post(url, data): req = urllib2.Request(url) data = urllib.……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3162浏览 1831个赞
python根据域名获得ip地址def getIp(domain): import socket myaddr = socket.getaddrinfo(domain,'http')[0][4][0] print(myaddr) 其中d……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3244浏览 638个赞
python 2.6 引入了itertools模块,使得排列组合的实现非常简单import itertools #排列:e.g., 4个数内选2个排列>>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), (1, 3), (1, 4), (2, 1), (2,……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3057浏览 2899个赞
Python从ftp服务器下载文件的代码#coding=utf-8''' ftp自动下载、自动上传脚本,可以递归目录操作'''from ftplib import FTPimport os,sys,string,datetime,timeimport socketclass MY……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1764浏览 1774个赞
Python的shelve模块提供了一种简单的数据存储方案,以dict(字典)的形式来操作数据。#!/usr/bin/pythonimport sys, shelvedef store_person(db): """ Query user for data and store it in the she……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2707浏览 2304个赞
python通过ip查询网站获得本地ip的代码import reimport urllib.requestclass GetIP(): def get_external_ip(self, nurl='http://city.ip138.com/city0.asp'): try: o……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1863浏览 2642个赞
python 通过logging写入日志到文件和控制台import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1719浏览 2163个赞
python通过socket进行通信的简单范例,带客户端和服务器端 服务器端from socket import *import threadPORT=5648BUFSIZE=1024def th(): while True: data=raw_input("……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1805浏览 402个赞
python编写的一个超级简单的密码生成器import randomimport stringpass_gen = lambda length, ascii = string.ascii_letters + string.digits + string.punctuation: "".join([list(set(ascii)……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2709浏览 2807个赞
python将字符串转换成数组的代码#------------------------------------------------------------------------------# Name: string_to_array.py# Author: Kevin Harris# Last Mod……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2478浏览 1210个赞
python复制文件的代码演示和详解 本文涉及到的有Python复制文件在实际应用操作方案的实际应用以及Python复制文件 的相关的代码的详解,如果你对其有兴趣的话,你就可以点击以下的文章浏览我们的文章,望你会有所收获。Python复制文件import shu……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2225浏览 704个赞
python内置了timeit模块,通过它可以很简单的计算出代码执行时间,可以通过number参数控制代码的执行次数,非常好用。更详细的实用方法可以参考:http://docs.python.org/2/library/timeit.html>>> import timeit>>> timeit.timeit(……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2031浏览 337个赞
python利用PIL给图片打水印水印import Image, ImageEnhancedef reduce_opacity(im, opacity): """Returns an image with reduced opacity.""" assert opacity ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1911浏览 2030个赞
如:int,str,char,float,ord,hex,oct等类型的相互转换代码int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real [,imag ]) 创……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3180浏览 259个赞
python通过点操作符和reduce遍历对象的属性class Klass(object): def __getattr__(self, name): """ Locate the function with the dotted attribute. ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3057浏览 2925个赞
python搜索指定目录的代码演示#------------------------------------------------------------------------------# Name: search_directory.py# Author: Kevin Harris# Last Mod……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2216浏览 2118个赞
python随机生成一个简单的密码代码import randomimport sysdef main(argv): if (len(sys.argv) != 2): sys.exit('Usage: simple_pass.py <password_length>') password = ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2400浏览 359个赞
Image对象有open方法却没有close方法,如果打开图片,判断图片高度和宽度,判断完成后希望删除或者给图片改名,是无法操作的,这段代码可以解决这个问题,注意open函数打开图片文件要使用二进制方式,及参数使用’rb’,有的文章给出的只有个’r’参数,Image是无法open的fileName = &……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3079浏览 1837个赞
本篇将介绍python中sys, getopt模块处理命令行参数 如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?需要模块:sys参数个数:len(sys.argv)脚本名: &nbs……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2003浏览 1393个赞
python获取从命令行输入的数字代码演示#------------------------------------------------------------------------------# Name: numerical_input.py# Author: Kevin Harris# Last ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3119浏览 2373个赞
python使用utf-8编码编写代码,只需要在.py文件头部加上这句代码即可,加上后可以支持中文# -*- coding: utf-8 -*-……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3449浏览 1574个赞
python中字典(dict)和字符串之间的相互转换 字典(dict)转为字符串(string)我们可以比较容易的将字典(dict)类型转为字符串(string)类型。通过遍历dict中的所有元素就可以实现字典到字符串的转换:for key, value in sample_d……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2983浏览 1136个赞
python中sleep函数用法演示,sleep用来暂停线程执行,单位为秒#------------------------------------------------------------------------------# Name: sleep.py# Author: Kevin Harris# ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2569浏览 2189个赞
下面的python代码用于监控本机的8080端口,当用于通过http请求,服务器返回固定的html代码import SocketServerclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print "From:&q……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2746浏览 2198个赞
python通过urlunsplit函数将网址的多个部分合成一个完整的urlimport urlparseprint urlparse.urlunsplit(('http','www.sharejs.org:80', '/faq.cgi','src=fie','……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2761浏览 2219个赞
python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信import SocketServerimport netstringimport sysimport stringclass MyRequestHandler(SocketServer……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2764浏览 123个赞
python连接url的不同部分,例如将http://www.75271.com/ 和 /other/path连接成一个完整的urlimport urlparseprint urlparse.urljoin('http://www.75271.com/', '/other/path')……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1595浏览 1610个赞
python中迭代器(iterator)的用法#------------------------------------------------------------------------------# Name: iterators.py# Author: Kevin Harris# Last Modi……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2339浏览 2586个赞
python编写的简单的socket serverimport sockethost = ''port = 55555myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)myServerSocket.setsockopt(socket.SOL……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1952浏览 2966个赞
下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 水墨上仙 6年前 (2021-01-15) 1971浏览 1580个赞
python执行get提交的操作import sys, urllib2, urllibdef addGETdata(url, data): """Adds data to url. Data should be a list or tuple consisting of 2-item lists o……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2184浏览 124个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2372浏览 2389个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3258浏览 1422个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2263浏览 1699个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2447浏览 1849个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 水墨上仙 6年前 (2021-01-15) 2636浏览 981个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3537浏览 273个赞
通过tcp实现字符大写转换的python客户端代码,服务端代码参考:http://www.75271.com/codes/python/8081import socketimport netstrings = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(('1……继续阅读 » 水墨上仙 6年前 (2021-01-15) 3483浏览 2000个赞