唐小风

安全工程师这些年

python简单编程

1:

#conding=utf-8

import requests
import re

url ='http://www.ichunqiu.com/courses'

headers={
  'Host': 'www.ichunqiu.com',

  'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.8.0',

  'Accept': '*/*',

  'Accept-Language': 'en-US,en;q=0.5',

  'Accept-Encoding': 'gzip, deflate',

  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',

  'X-Requested-With': 'XMLHttpRequest',

  'Referer': 'http://www.ichunqiu.com/courses',
}
r=requests.get(url=url,headers=headers)
print r.text

 

 

 

2:

#coding=utf-8

import threading
import time
import requests
import sys

def fun1():
time_start=time.time()
r=requests.get(url='https://www.baidu.com')
times=time.time()-time_start
sys.stdout.write('Status:%s----%s-----%s\n'%(r.status_code,times,time.strftime('%H:%M:%S'))) 

def main():
threads = []
 

threads_count=10
  
for i in range(threads_count):
  t=threading.Thread(target=fun1,args=())
  threads.append(t)

for i in range(threads_count):
  threads[i].start()

for i in range(threads_count):
  threads[i].join()

if __name__=='__main__':
main()

 

 

 

 

 

 

3:

#conding=utf-8

import threading
import Queue
from subprocess import Popen,PIPE
import sys
class DoRun(threading.Thread):
def __init__(self,queue):
  threading.Thread.__init__(self)
  self._queue=queue


def run(self):
  while not self._queue.empty():
   ip=self._queue.get()
   #print ip
   check_ping=Popen(['/bin/bash','-c','ping -c 2 '+ip],stdin=PIPE,stdout=PIPE)
   data=check_ping.stdout.read()
   if'ttl' in data:
    sys.stdout.write(ip+'is up\n') 
def main():
threads=[]
threads_count=100
queue=Queue.Queue()

for i in range(1,255):
  queue.put('14.215.177.'+str(i))
 
for i in range(threads_count):
  threads.append(DoRun(queue))

for i in threads:
  i.start()
for i in threads:
  i.join() 

if __name__=='__main__':
main()

 

 

评论