代码改变世界

随笔分类 -  python(原创)

服务器上的文件的上传和下载

2015-10-28 13:25 by Rollen Holt, 2294 阅读, 收藏, 编辑
摘要: 服务器上的文件的上传和下载我们有很多种方式上传文件到服务器上,比如 , 比如使用命令行工具: , 等等的。我之前经常使用的是 这个命令,但是在Mac下经常乱码然后卡住不动了,所以我很崩溃。下面说说我现在采用的办法吧。 下载文件 python mSimpleHTTPServer9999 上传... 阅读全文

windows下apache+wsgi+web.py环境搭建

2012-08-09 23:23 by Rollen Holt, 7250 阅读, 收藏, 编辑
摘要: 首先安装好wsgi模块并启用:1.下载地址:我本机是python2.7http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-win32-ap22py27-3.3.so2.把mod_wsgi-win32-ap22py27-3.3.so放到apache安装目录下的modules目录下3.打开 http.conf添加:LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so下载安装web.py模块:easy_install -U web.py或者手动下载安装:1 阅读全文

Code a network packet sniffer in python for Linux

2012-07-14 00:17 by Rollen Holt, 7983 阅读, 收藏, 编辑
摘要: Basic SnifferThe most basic form of a sniffer would be :1#Packet sniffer in python2#For Linux34importsocket56#create an INET, raw socket7s=socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)89# receive a packet10whileTrue:11prints.recvfrom(65565)Run this with root privileges or sudo o 阅读全文

Python标准库-string模块《未完待续》

2012-06-05 22:08 by Rollen Holt, 2155 阅读, 收藏, 编辑
摘要: >>> import string>>> s='hello rollen , how are you '>>> string.capwords(s)'Hello Rollen , How Are You' #每个单词的首字母大写>>> string.split(s)['hello', 'rollen', ',', 'how', 'are', 'you'] #划分为列... 阅读全文

python操作MySQL数据库

2012-05-29 17:41 by Rollen Holt, 369063 阅读, 收藏, 编辑
摘要: 坚持每天学一点,每天积累一点点,作为自己每天的业余收获,这个文章是我在吃饭的期间写的,利用自己零散的时间学了一下python操作MYSQL,所以整理一下。我采用的是MySQLdb操作的MYSQL数据库。先来一个简单的例子吧:import MySQLdbtry: conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306) cur=conn.cursor() cur.execute('select * from use 阅读全文

理解Python中的装饰器

2012-05-02 23:07 by Rollen Holt, 43672 阅读, 收藏, 编辑
摘要: 文章先由stackoverflow上面的一个问题引起吧,如果使用如下的代码: @makebold@makeitalicdef say(): return "Hello"打印出如下的输出: <b><i>Hello<i></b>你会怎么做?最后给出的答案是: def makebold(fn): def wrapped(): return "<b>" + fn() +... 阅读全文

用PDB库调试Python程序

2012-04-24 23:22 by Rollen Holt, 3050 阅读, 收藏, 编辑
摘要: 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数,这样调用myscript.py的话断点就是程序的执行第一行之前 python -m pdb myscript.py 2. 在Python交互环境中启用调试 >>> import pdb >>> import mymodule >>> pdb.run(‘mymodule.test()’) 在 Python... 阅读全文

关于python文件操作

2012-04-23 11:28 by Rollen Holt, 424021 阅读, 收藏, 编辑
摘要: 总是记不住API。昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧:python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd()返回指定目录下的所有文件和目录名:os.listdir()函数用来删除一个文件:os.remove()删除多个目录:os.removedirs(r“c:\python”)检验给出的路径是否是一个文件:os.path.isfile()检验给出的路径是否是一个目录:os.path.isdir()判断是否是绝对路径:os.path.isabs()检验给出的路径是 阅读全文

python日期操作

2012-04-11 07:38 by Rollen Holt, 39279 阅读, 收藏, 编辑
摘要: datetime模块定义了下面这几个类: datetime.date:表示日期的类。常用的属性有year, month, day.datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond.datetime.datetime:表示日期时间。datetime.timedelta:表示时间间隔,即两个时间点之间的长度。datetim... 阅读全文

Python日期操作

2012-04-10 23:30 by Rollen Holt, 35904 阅读, 收藏, 编辑
摘要: 1. 日期输出格式化 所有日期、时间的api都在datetime模块内。 1. datetime => string now = datetime.datetime.now()now.strftime('%Y-%m-%d %H:%M:%S')输出2012-03-05 16:26:23.870105strftime是datetime类的实例方法。2. string => datetimet... 阅读全文

"仿matlab科学软件"项目准备

2011-12-08 17:11 by Rollen Holt, 950 阅读, 收藏, 编辑
摘要: 最近会发一些小的零散的文章,为自己的寒假的项目做准备#coding=utf-8import matplotlib.pyplot as pltfig=plt.figure("hello") #设置标题栏fig.suptitle("bold figure suptitle",fontsize=12,fontweight="bold")ax = fig.add_subplot(111)fig.subplots_adjust(top=0.85)ax.set_title('axes title')ax.set_xlabel(&q 阅读全文

python操作gmail

2011-12-08 13:48 by Rollen Holt, 1599 阅读, 收藏, 编辑
摘要: import imaplib, reclass pygmail(object): def __init__(self): self.IMAP_SERVER='imap.gmail.com' self.IMAP_PORT=993 self.M = None self.response = None self.mailboxes = [] def login(self, username, password): self.M = imaplib.IMAP4_SSL(self.IMAP_SERVER, self.IMAP_PORT) rc, self.response = self. 阅读全文

BeautifulSoup学习笔记

2011-12-01 23:08 by Rollen Holt, 976 阅读, 收藏, 编辑
摘要: from BeautifulSoup import BeautifulSoupimport redoc = ['<html><head><title>Page title</title></head>', '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.', '<p id="secondpara" 阅读全文

python的xml.dom学习笔记

2011-12-01 20:14 by Rollen Holt, 5015 阅读, 收藏, 编辑
摘要: 首先说一下,由于这篇文章主要是自己随性学习写的,所以读者看起来可能很乱,呵呵。可以给大家稍微推荐一篇:http://www.cnblogs.com/xuxm2007/archive/2011/01/16/1936610.html稍微清晰一点#coding=utf-8#解析xml文件中的所有的link标签from xml.dom import minidomfrom xml.dom.minidom import getDOMImplementationdoc=minidom.parse("d:\\hello.html")nodes=doc.getElementsByTagNa 阅读全文

python的HTMLParser学习

2011-12-01 18:55 by Rollen Holt, 15093 阅读, 收藏, 编辑
摘要: 先来大致看看HTMLParser的源代码吧:"""A parser for HTML and XHTML."""# This file is based on sgmllib.py, but the API is slightly different.# XXX There should be a way to distinguish between PCDATA (parsed# character data -- the normal case), RCDATA (replaceable character# data -- on 阅读全文

python标准库学习9

2011-11-27 12:26 by Rollen Holt, 974 阅读, 收藏, 编辑
摘要: fileinput模块允许你循环一个或多个文本文件的内容使用 fileinput 模块循环一个文本文件import fileinputimport sysfor line in fileinput.input("samples/sample.txt"): sys.stdout.write("-> ") sys.stdout.write(line)-> We will perhaps eventually be writing only small-> modules which are identified by name as the 阅读全文

python标准库学习8

2011-11-27 11:30 by Rollen Holt, 1627 阅读, 收藏, 编辑
摘要: 使用sys重定向输出import sysimport stringclass Redirect: def _ _init_ _(self, stdout): self.stdout = stdout def write(self, s): self.stdout.write(string.lower(s))# redirect standard output (including the print statement)# 重定向标准输出(包括print语句)old_stdout = sys.stdoutsys.stdout = Redirect(s... 阅读全文

python标准库学习7

2011-11-26 22:30 by Rollen Holt, 4229 阅读, 收藏, 编辑
摘要: 使用 os.path 模块处理文件名import osfilename = "my/little/pony"print "using", os.name, "..."print "split", "=>", os.path.split(filename)print "splitext", "=>", os.path.splitext(filename)print "dirname", "=>", o 阅读全文

python标准库学习6

2011-11-26 20:49 by Rollen Holt, 2651 阅读, 收藏, 编辑
摘要: 使用 apply 函数def function(a, b): print a, bapply(function, ("whither", "canada?"))apply(function, (1, 2 + 3)) whither canada? 1 5使用 apply 函数传递关键字参数def function(a, b): print a, bapply(function, ("crunchy", "frog"))apply(function, ("crunchy",), {"b& 阅读全文

python标准库学习5 ---bisect — Array bisection algorithm

2011-11-26 13:19 by Rollen Holt, 1636 阅读, 收藏, 编辑
摘要: #coding=utf-8import bisectlist=[1,2,3,4,6,7,8,9] #假定list已经排序print bisect.bisect_left(list,5) #返回5应该插入的索引位置print bisect.bisect_right(list, 5)print bisect.bisect(list,5)bisect.insort_left(list, 5, 0, len(list))print listbisect.insort_right(list, 5)print listdef index(a, x): 'Locate the leftmost .. 阅读全文