Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/env pythonimport sysfrom PyQt4 import QtGui,QtCoreimport httplibfrom urllib import urlencodeimport redef out(text): p = re.compile(r'","') m = p.split(text) result=unicode(m[0][4:].decode('utf-8'))
Time of Update: 2016-06-16
簡單實現平面的點K均值分析,使用歐幾裡得距離,並用pylab展示。複製代碼 代碼如下:import pylab as pl#calc Euclid squiredef calc_e_squire(a, b): return (a[0]- b[0]) ** 2 + (a[1] - b[1]) **2#init the 20 pointa = [2,4,3,6,7,8,2,3,5,6,12,10,15,16,11,10,19,17,16,13]b = [5,6,1,4,2,4,3,1,7,9,
Time of Update: 2016-06-16
使用Python如何操作Redis呢?下面用執行個體來說明用Python讀寫Redis資料庫。比如,我們插入一條資料,如下:複製代碼 代碼如下:import redisclass Database: def __init__(self): self.host = 'localhost' self.port = 6379 def write(self,website,city,year,month,day,deal_number):
Time of Update: 2016-06-16
複製代碼 代碼如下:#! /bin/bashfiles_url=(https://pypi.python.org/packages/source/s/setuptools/setuptools-2.1.tar.gz#md5=2044725530450d0517393882dc4b7508 https://pypi.python.org/packages/source/p/pip/pip-1.5.tar.gz)files=(setuptools
Time of Update: 2016-06-16
複製代碼 代碼如下:from selenium import seleniumclass MySpider(CrawlSpider): name = 'cnbeta' allowed_domains = ['cnbeta.com'] start_urls = ['http://www.jb51.net'] rules = ( # Extract links matching 'category.php' (but not matching
Time of Update: 2016-06-16
很多情境為了不阻塞,都需要非同步回調機制。這是一個簡單的例子,大家參考使用吧複製代碼 代碼如下:#!/usr/bin/env python# -*- coding: UTF-8 -*-import loggingimport queueimport threadingdef func_a(a, b): return a + bdef func_b(): passdef func_c(a, b, c): return a, b, c# 非同步任務隊列_task_queue =
Time of Update: 2016-06-16
python實現兩個文本合并employee檔案中記錄了工號和姓名複製代碼 代碼如下:cat employee.txt:100 Jason Smith200 John Doe300 Sanjay Gupta400 Ashok Sharmabonus檔案中記錄工號和工資複製代碼 代碼如下:cat bonus.txt:100 $5,000200 $500300 $3,000400 $1,250要求把兩個檔案合并並輸出如下, 處理結果:複製代碼 代碼如下:400 ashok sharma $1,250
Time of Update: 2016-06-16
今天在看其他同事的代碼時,發現一個沒使用過的python關鍵字 :yield 先問了一下同事,聽他說了幾句,有個模糊的印象,僅僅是模糊而已。於是自己去搜搜資料看。看了半天,逐漸清晰了。不過在工作機制以及應用上還是有點迷茫。嗯,先把初始接觸的印象記下來吧。 yield 簡單說來就是一個產生器(Generator)。產生器是這樣一個函數:它記住上一次返回時在函數體中的位置。對產生器函數的第二次(或第 n 次)調用跳轉至該函數中間,而上次調用的所有局部變數都保持不變。
Time of Update: 2016-06-16
最近為了測試網速情況怎麼樣,由於部分商務服務器需要關閉icmp,這樣的話採用普通的ping就無法適應我的需求,於是自己簡單的寫了一個基於tcp連接埠的ping的程式,由於c執行效率比較的不錯,但是開發效率低下,而python是開發效率高,但是執行效率不如C,由於需要大規模的使用,於是用C實現核心部分的代碼,並把這部分實現成一個python的模組,由python調用c的模組,下面就貼代碼吧複製代碼 代碼如下:/* tcpportping.c */#include #include
Time of Update: 2016-06-16
下面開始構造HTTP資料包,IP層和TCP層使用python的Impacket庫,http內容自行填寫。複製代碼 代碼如下:#!/usr/bin/env python#-------------------------------------------------------------------------------# Name: raw_http.py# Purpose: construct a raw http get packet## Author:
Time of Update: 2016-06-16
複製代碼 代碼如下:from win32com.client import DispatchEximport timeie=DispatchEx("InternetExplorer.Application")ie.Navigate("http://hi.baidu.com/mirguest/creat/blog/")ie.Visible=1while ie.Busy: time.sleep(1)body=ie.Document.body# headerfor i in
Time of Update: 2016-06-16
複製代碼 代碼如下:import osdef load() : filename = os.getcwd() + r'\fromto.txt' if os.path.isfile(filename) : f = open(filename) try : lines = f.readlines() finally : f.close() return lines
Time of Update: 2016-06-16
首先是,從網路攝影機截取一幅映像:複製代碼 代碼如下:while 1: #測試網路攝影機的存在 try: cam = Device() except: print "no webcam found!" continue break然後是把映像上傳到ftp伺服器:複製代碼 代碼如下:remote = ftplib.FTP('127.0.0.1') #登陸伺服器remote.login()file =
Time of Update: 2016-06-16
複製代碼 代碼如下:#_*_coding:utf_8_from sgmllib import SGMLParserimport sys, urllib2, urllib, cookielibimport datetime, timeclass spider(SGMLParser): def __init__(self, email, password): SGMLParser.__init__(self) self.email = email
Time of Update: 2016-06-16
複製代碼 代碼如下:#!/usr/bin/python #-*-coding:utf-8-*-from push_button import *from clabel import *from common import *from PyQt4.QtGui import *from PyQt4.QtCore import *from PyQt4.Qt import *class CharacterWidget(QWidget): def __init__(self,parent = None)
Time of Update: 2016-06-16
crtrl.py監控Apache伺服器處理序的Python 指令碼複製代碼 代碼如下:!/usr/bin/env Python import os, sys, time while True: time.sleep(4) try: ret = os.popen('ps -C apache -o pid,cmd').readlines() if len(ret) print "apache 進程異常退出, 4 秒後重新啟動" time.sleep(3) os.system("service
Time of Update: 2016-06-16
複製代碼 代碼如下:import urllib.parse,os.path,time,sysfrom http.client import HTTPSConnectionfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *#pathospath=sys.path[0]if len(ospath)!=3: ospath+='\\'ospath=ospath.replace('\\','
Time of Update: 2016-06-16
python發送icmp echo requesy請求複製代碼 代碼如下:import socketimport structdef checksum(source_string): sum = 0 countTo = (len(source_string)/2)*2 count = 0 while count thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
Time of Update: 2016-06-16
一、python中對檔案、檔案夾操作時經常用到的os模組和shutil模組常用方法。1.得到當前工作目錄,即當前Python指令碼工作的目錄路徑:
Time of Update: 2016-06-16
Python break語句,就像在C語言中,打破了最小封閉for或while迴圈。break語句用來終止迴圈語句,即迴圈條件沒有False條件或者序列還沒被完全遞迴完,也會停止執行迴圈語句。break語句用在while和for迴圈中。如果您使用嵌套迴圈,break語句將停止執行最深層的迴圈,並開始執行下一行代碼。一、Python語言 break 語句文法複製代碼 代碼如下:break二、邏輯流程圖三、使用執行個體複製代碼 代碼如下:#!/usr/bin/pythonfor letter in