转自 http://www.lixiaodou.cn/?cat=8 # -*- coding: utf-8 -*- def charu(list): length = len(list) for i in range(1,length): temp = list j = i - 1 while list[j] > temp: list[j+1] = list[j] list[j] = temp j = j - 1 if j 0: break return list def xuanze(list): length = len(list) for i in range...
by BENNYSNAKE - Python文档中心 - 2009-05-25 18:05:36 阅读(1258) 回复(0)
爬虫工作的基本原理就是,给定一个初始的url,下载这个url的网页,然后找出网页上所有满足下载要求的链接,然后把这些链接对应的url下载下来,然 后再找下载下来的这些网页的url,我们可以用广度优先搜索实现这个算法. #!/usr/bin/python import urllib2 import re def downURL(url,filename): print url print filename try: fp = urllib2.urlopen(url) except: print 'download exception' ...
用python来实现> 最近在学习python,又不知道写个什么来练手,就想到用python来实现>里面的例程, <一> 第二章,例2-3的清除函数: #! /usr/bin/env python # -*- coding:utf-8 -*- #TO: #BY: #File: cleanup.py #Date: 2008-00-12/26/08 # import os import sys LOG_DIR="/var/log" ROOT_UID=0 LINES=50 E_XCD=65 E_NOTROOT=67 if os.getuid() != ROOT_UID: print "Must be root to run this script." sys.exit(E...
Example 4: Koch curveA variant of the Koch curve which uses only right-angles. variables : F constants : + − start : F rules : (F → F+F−F−F+F) Here, F means "draw forward", + means "turn left 90°", and - means "turn right 90°"(see turtle graphics). n = 0: F n = 1: F+F-F-F+F n = 2: F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+F-F-F+F+F+F-F-F+F n = 3: F+F-F-F+F+F+F-F-F+F-F+F-F-F+F-F+...
#!/usr/bin/python import smtplib, email import os, sys import hashlib def send_mail(send_from, send_to, subject, text, attachment_bytes, auth=(), send_server='localhost'): msg = email.MIMEMultipart.MIMEMultipart() msg['From'] = send_from msg['To'] = email.Utils.COMMASPACE.join(send_to) msg['Date'] = email.Utils.formatdate(localtime=True) msg['Subject'] = subject msg.atta...
#!/usr/bin/python import sendpkt import dpkt import os import re import socket import struct import string import sys import signal iface = "eth0" mac = "00:09:5B:98:0D:85" inet = "10.29.1.61" debug = False # this should be somewhere is dpkt ? ETH_ADDR_BROADCAST = '\xff\xff\xff\xff\xff\xff' ETH_ADDR_UNSPEC = '\x00\x00\x00\x00\x00\x00' def eth_ntoa(buffer): # Convert binary data into a stri...
#!/usr/bin/env python """ping.py ping.py uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. Copyright (C) 2004 - Lars Strand ; This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at you...
UDP协议相比TCP要简单许多,虽然数据无法保证完整性. 先看一下client端的演示代码: import socket s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) port = 8000 host = '192.168.1.102' while True: msg = raw_input() if not msg: break s.sendto(msg,(host,port)) s.close() 注意,在创建socket的时候,第二个参数要为SOCK_DGRAM,然后,我们只需要调用sendto即可以了,真是太方便了. 再看看serve...
1.有时间就来研究研究python中的lib包下面的所有模块代码,写得相当有水平的! 实现一个简单的HTTP服务器 #Copyright Jon Berg , turtlemeat.com import string,cgi,time from os import curdir, sep from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer #import pri class MyHandler(BaseHTTPRequestHandler): def do_GET(self): try: if self.path.endswith(".html"): f = ...
webdir = '.' # where your HTML files and cgi-bin script directory live port = 80 # http://servername/ if 80, else use http://servername:xxxx/ import os, sys from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler #处理CGI程序的 if len(sys.argv) > 1: webdir = sys.argv[1] # command-line args if len(sys.argv) > 2: port = int(sys.argv[2]) ...
There is currently no switch statement in python. Often this is not a problem and can be handled through a series of if-elif-else statements. However, there are many other ways to handle the deficiency. The following example shows how to create a simple switch statement in python: def a(s): print s def switch(ch): try: {'1': lambda : a("one"), '2': lambda : a(...