码迷,mamicode.com
首页 > 编程语言 > 详细

【python】python实例集<二>

时间:2015-12-14 18:58:36      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

##扫描某个ip的端口号
# #-*- coding: utf-8 -*-
# import socket
# def main():
#     sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#     sk.settimeout(1000)
#     ip=raw_input(‘请输入目标主机:(默认:127.0.0.1)‘)
#     if ip==‘‘:
#         ip=‘127.0.0.1‘
#     print ip
#     s=raw_input(‘请输入目标主机开始端口:(默认:80)‘)
#     if s==‘‘:
#         startport=80
#     else:
#         startport=int(s)
#
#     s=raw_input(‘请输入目标主机结束端口:(默认:80)‘)
#     if s==‘‘:
#         endport=80
#     else:
#         endport=int(s)
#
#     for port in range(startport,endport+1):
#         print(‘正在扫描端口:%d‘ % port)
#         try:
#             sk.connect((ip,port))
#             print(‘Server %s port %d OK!‘ % (ip,port))
#         except Exception:
#             print(‘Server %s port %d is not connected!‘ % (ip,port))
#     sk.close()
#
# if __name__ == ‘__main__‘:
#     main()

##在windows命令行下输出彩色文字

#!/usr/bin/env python  
#encoding: utf-8
import ctypes

STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12

FOREGROUND_BLACK = 0x0
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN= 0x02 # text color contains green.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.

BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN= 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.

class Color:
    ‘‘‘ See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
    for information on Windows APIs. - www.sharejs.com‘‘‘
    std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    
    def set_cmd_color(self, color, handle=std_out_handle):
        """(color) -> bit
        Example: set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        """
        bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
        return bool
    
    def reset_color(self):
        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
    
    def print_red_text(self, print_text):
        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY)
        print print_text
        self.reset_color()
        
    def print_green_text(self, print_text):
        self.set_cmd_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
        print print_text
        self.reset_color()
    
    def print_blue_text(self, print_text): 
        self.set_cmd_color(FOREGROUND_BLUE | FOREGROUND_INTENSITY)
        print print_text
        self.reset_color()
          
    def print_red_text_with_blue_bg(self, print_text):
        self.set_cmd_color(FOREGROUND_RED | FOREGROUND_INTENSITY| BACKGROUND_BLUE | BACKGROUND_INTENSITY)
        print print_text
        self.reset_color()    

if __name__ == "__main__":
    clr = Color()
    clr.print_red_text(‘red‘)
    clr.print_green_text(‘green‘)
    clr.print_blue_text(‘blue‘)
    clr.print_red_text_with_blue_bg(‘background‘)

 ##机器喇叭发出蜂鸣声

import winsound
winsound.Beep(600,1000)  #其中600表示声音大小,1000表示发生时长,1000为1秒

 ##多线程不断刷新网页

#-*-coding:utf-8-*-
import thread
import urllib2
import sys
import time

def usage():
    print ‘Usage: python ‘ + sys.argv[0] + ‘ <url> <threads>‘
    sys.exit()

def reloader(numthread):
    url = sys.argv[1]
    numreloads = 0
    while True:
        try:
            urllib2.urlopen(url)
            numreloads = numreloads + 1
        except KeyboardInterrupt:
            sys.exit(‘\nProcess aborted.‘)

def splash():
    print ‘welcome to http://www.sharejs.com/codes ‘

if len(sys.argv) < 3:
    usage()

if __name__ == ‘__main__‘:
    splash()
    print ‘[!] DoSing ‘ + sys.argv[1] + ‘ with ‘ + sys.argv[2] + ‘ threads.‘
    for reloadspawn in range(0, int(sys.argv[2])):
        thread.start_new_thread(reloader, (reloadspawn,))##循环新开进程
    sys.stdout.write(‘‘)
    dosind = [‘-‘, ‘\\‘, ‘|‘, ‘/‘]
    dosstat = 0
    while True:
        try:
            sys.stdout.write("\r"+ dosind[dosstat % 4] + ‘ DoSing...‘)
            sys.stdout.flush()
            dosstat = dosstat + 1
            time.sleep(0.25)
        except KeyboardInterrupt:
            sys.exit(‘\nProcess aborted.‘)

##目录下指定扩展名的文件

import os
items = os.listdir(".")

newlist = []
for names in items:
    if names.endswith(".txt"):
        newlist.append(names)
print newlist

##正则表达式提取网页URL

import re
import urllib
url="http://www.itokit.com"
s=urllib.urlopen(url).read()
ss=s.replace(" ","")
urls=re.findall(r"<a.*?href=.*?<\/a>",ss,re.I)
for i in urls:
print i
else:
print ‘this is over‘

  

【python】python实例集<二>

标签:

原文地址:http://www.cnblogs.com/paulwinflo/p/5045576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!