python编写的超简单端口转发程序代码非常简单,实现了简单的端口数据转发功能,用于真实环境还需要再修改一下。转自:http://blog.csdn.net/qffhq#tcp server import socket host = '127.0.0.1' #Local Server IP h……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1763浏览 1576个赞
xapian 通过python创建索引数据库的复杂一些的范例除了包含索引字符串外,还为索引添加了两个值#!/usr/bin/env python## Index each paragraph of a text file as a Xapian document.# Include some values that will be of use……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1362浏览 123个赞
这段代码需要调用serial模块,通过while循环不断读取串口数据转自:http://www.yelinsky.com/import timeimport serialser = serial.Serial( #下面这些参数根据情况修改 port='COM1', baudrate=9600, par……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2299浏览 1335个赞
python通过BF算法实现关键词匹配作者:smalltt#!/usr/bin/python# -*- coding: UTF-8# filename BFimport time"""t="this is a big apple,this is a big apple,this is a big ap……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1969浏览 948个赞
python利用asyncore的端口映射(端口转发)可以实现端口数据的转发,效果还是不错的代码转自:http://www.cnblogs.com/nethirteimport socket,asyncoreclass forwarder(asyncore.dispatcher): def __init__(self, ip, port……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1747浏览 2597个赞
python读写文件方法汇总转自:http://blog.csdn.net/adupt/archive/2009/08/11/4435615.aspx 1.open使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2155浏览 2603个赞
python getaddrinfo() 基本使用代码import sys, socketresult = socket.getaddrinfo("192.1.1.100", None)print result[0][4]print result 输出结……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1728浏览 2460个赞
这段代码可以生成5个不同的随机字母组成的长度为8的字符串import random, string for c in range(5): print random.sample(string.letters+string.digits, 8)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2452浏览 2741个赞
python实现一个简单的基于SSL的 IRC bot的代码#!/usr/bin/python# -*- coding: utf8 -*- import socket, string, time, sslimport urllib, re network = 'irc.server.net'nick = 'n……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1924浏览 131个赞
使用urllib和正则抓取和分析网页的简单代码片段import urllib, re url = 'http://www.viedemerde.fr/aleatoire'page = urllib.urlopen(url).read()parse = re.findall("\<div class=\&quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2253浏览 1742个赞
python编写的简单RPG游戏流程代码#RPGrpg = Truewhp = 100mahp = 100hhp = 100MHP = 10 def dgrnd () : wa = raw_input ("What does Warrior do?") ma = raw_input ("Wha……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2257浏览 2654个赞
将代码生成.py文件放在目录下运行,可以获取该目录的所有mp3文件的信息,需要使用ID3库import os, sysfrom ID3 import * files = os.listdir(os.getcwd()) for f in files: x = os.path.splitext(f) if x[1] == ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2903浏览 2198个赞
python将html表格转换成CSV使用方法:python html2csv.py *.html这段代码使用了 HTMLParser 模块#!/usr/bin/python# -*- coding: iso-8859-1 -*-# Hello, this program is written in Python - http://python.o……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1995浏览 301个赞
python根据主机名字获得所有ip地址# -*- coding: utf-8 -*-import sys, socketresult = socket.getaddrinfo('www.google.com', None, 0, socket.SOCK_STREAM)counter = 0for item in resul……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1481浏览 2329个赞
一个简单python ftp客户端代码#!/usr/bin/python# -*- coding: utf-8 -*-import ftplibimport osimport socketHOST = 'ftp.mozilla.org'DIRN = 'pub/mozilla.org/webtools'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2333浏览 2314个赞
一段简单的python邮件客户端发送代码#/usr/bin/python# -*- coding: utf-8 -*-import reimport smtplibfrom poplib import POP3from email.mime.text import MIMETextdef sevname(username): pa ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2679浏览 2444个赞
python实现自动登录人人网并采集信息的代码#!/usr/bin/python# -*- coding: utf-8 -*-import sysimport reimport urllib2import urllibimport cookielibclass Renren(object): def __init__(se……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3045浏览 1454个赞
python代码用于查找指定具有相同内容的文件,可以同时指定多个目录调用方式:python doublesdetector.py c:\;d:\;e:\ > doubles.txt# Hello, this script is written in Python - http://www.75271.com# doublesdetector.py 1……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1681浏览 2925个赞
python Gevent multiprocessing serverimport sysfrom gevent import serverfrom gevent.baseserver import _tcp_listenerfrom gevent.monkey import patch_all; patch_all()from multi……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3164浏览 1668个赞
一个使用进程内通讯的python聊天室代码,非常简单的服务端和客户端代码#!/usr/bin/env python# Added by <ctang@redhat.com>import sysimport osfrom multiprocessing import connectionADDR = ('', 9……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1781浏览 825个赞
这段代码可以对指定的目录进行扫描,包含子目录,对指定扩展名的文件进行SHA-1加密后存储在cvs文件,以防止文件被篡改调用方法:python snapper.py > todayCheck.csv# Hello, this is a script written in Python. See http://www.pyhon.org## Snapp……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1507浏览 2714个赞
我用这段代码来压缩数据库备份文件,没有使用python内置的zip模块,而是使用了zip.exe文件# Hello, this script is written in Python - http://www.75271.com## autozip.py 1.0p## This script will scan a directory (and……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1487浏览 2423个赞
这段python可以用来读取新闻组内的新闻,并保存为纯文本文件# Hello, this script is written in Python - http://www.75271.com## newsarchiver 1.1p - Newsgroup archiver## Purpose:# This script will dow……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1253浏览 854个赞
python打印ASCII编码字符代码i = 0while i < 256: print chr(i), if i != 0 and i % 8 == 0 : print i = i + 1……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2921浏览 343个赞
python实现的矩阵乘法def matrixMul(A, B): res = [[0] * len(B[0]) for i in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in range(le……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1724浏览 2001个赞
There is always room for optimizing primelist functions. Here is an assortment timed with Python module timeit …'''primelist_timing1.pytiming some very fas……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1445浏览 2775个赞
This snippet allows one to implement an equivalent of a __getattr__() function in a python module. The module can then define its own way to lazily import abitrary symbols. For exa……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1728浏览 537个赞
python从任意文件读取邮件地址输出,包括html、txt甚至exe文件调用方法:python email_extractor.py < PythonFAQ.html# Hello, this script is written in Python - http://www.75271.com# Script written by Sebastie……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1709浏览 2112个赞
python 操作 ssh#coding:utf-8""" ssh操作例子 实现了服务器日志下载 2012-08-24 yywolf"""import paramikoimport timehostname="????"port=22u……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2256浏览 1925个赞
python统计cpu的利用率#-*-coding=utf-8-*-import win32pdhimport time# Counter pathsPROCESSOR_PERCENT = r'\Processor(_Total)\% Processor Time'MEMORY_PERCENT = r'\Memo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2481浏览 1703个赞
python 使用socket远程发送命令并获得执行结果 #socket_server.pyimport socketimport osimport sysdef work(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3116浏览 2452个赞
python实现线程池原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码出处:http://blog.csdn.net/liujian0616/article/details/7951081import threadingimport timeimport signalimport oscla……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3003浏览 2179个赞
用python实现windows服务-在服务中新建进程出处:http://blog.csdn.net/liujian0616/article/details/7950758 需要安装的软件:python和pywin32,我这里装的分别是python-2.6.amd64、pywin32-……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1828浏览 1043个赞
python xmlrpc实现二进制文件传输的代码 服务器端from SimpleXMLRPCServer import SimpleXMLRPCServerimport xmlrpclib def python_logo(): handle = open(&quo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3025浏览 1779个赞
python urllib.urlencode用法演示>>> import urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> f =……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1860浏览 1521个赞
python写的FTP简单上传下载文件出自 “王伟” 博客,请务必保留此出处http://wangwei007.blog.51cto.com/68019/983638#!/usr/bin/env python # -*- coding: utf-8 -*- from ftplib import FTP def ftp_up(filenam……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2984浏览 696个赞
通过python自动连接ssh服务器#!/usr/bin/python#-*- coding:utf-8 -*-import sys, time, ostry: import pexpectexcept ImportError: print """ You must install ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2166浏览 2454个赞
python判断文件和文件夹是否存在import osos.path.isfile('test.txt') #如果不存在就返回Falseos.path.exists(directory) #如果目录不存在就返回False……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2073浏览 2561个赞
python通过urllib2提交http post请求#!/usr/bin/python #coding=utf-8 import urllib import urllib2 def post(url, data): req = urllib2.Request(url) data = urllib.……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1618浏览 546个赞
python根据域名获得ip地址def getIp(domain): import socket myaddr = socket.getaddrinfo(domain,'http')[0][4][0] print(myaddr) 其中d……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1708浏览 1079个赞
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,……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1791浏览 2816个赞
Python从ftp服务器下载文件的代码#coding=utf-8''' ftp自动下载、自动上传脚本,可以递归目录操作'''from ftplib import FTPimport os,sys,string,datetime,timeimport socketclass MY……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2790浏览 1685个赞
Python的shelve模块提供了一种简单的数据存储方案,以dict(字典)的形式来操作数据。#!/usr/bin/pythonimport sys, shelvedef store_person(db): """ Query user for data and store it in the she……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3090浏览 755个赞
python通过ip查询网站获得本地ip的代码import reimport urllib.requestclass GetIP(): def get_external_ip(self, nurl='http://city.ip138.com/city0.asp'): try: o……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1911浏览 2629个赞
python 通过logging写入日志到文件和控制台import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3060浏览 899个赞