用python寫的一個網頁測試載入器

來源:互聯網
上載者:User

寫了一個測試網站的指令碼,用多線程跑的,每個線程每秒去取一次網頁,因為這個指令碼用到了線程啊,time,ulrlib這些東西,也許對其他人會有用,貼出來吧。

另外一個原因是,通過這個指令碼,發現python的效率真的不錯,無論是開發效率還是執行效率。之前完全沒用過python的thread和time,一邊看python programing,一邊寫,也就一個小時的功夫,看上去效果還不錯。

代碼如下:

 

#!/usr/local/bin/python
#FileName = test_Web.py

#get from db
#http://192.168.1.74/spaces/posts/postdetail.aspx?id=
#put into db
#http://192.168.1.74/admin/space/post/post_add.aspx?fid=0&um=300372&v=__VERSION__&title=0&content=hahaasdfasdf

import thread, time, urllib

id_index = 100000
id_count = 20000
id_max = id_index+id_count
i_cnt = 0
time_begin = time.time()
bStop = False

def openurl():
 sock = urllib.urlopen("http://192.168.1.74/admin/space/post/post_add.aspx?fid=0&um=300372&v=__VERSION__&title=0&content=hahaasdfasdf")
 htmlSource = sock.read()
 #print htmlSource
 sock.close() 
 
def opengeturl(id):
 strUrl = "http://192.168.1.74/spaces/posts/postdetail.aspx?id=%d" % id
 print strUrl
 sock = urllib.urlopen(strUrl)
 htmlSource = sock.read()
 #print htmlSource
 sock.close() 

def child( myID ):        # this function runs in threads
 while( True ):
  global id_index
  global id_max
  global id_count
  global time_begin
  global bStop
  global i_cnt
  
  if( i_cnt > id_count ):
   now = time.time()
   i_handle_time = now - time_begin
   if( bStop == False ):
    print "%d rows cost %f second" % ( id_count, i_handle_time )
   bStop = True
   break
  #id_index = id_index + 1
  i_cnt = i_cnt + 1
  print "[%d] ==> %d" % (myID, id_index)
  opengeturl(id_index)
  time.sleep(1)

for i in range( 500 ):       # spawn 3 threads
 thread.start_new( child, (i,) )
 
time.sleep(1000000)
print 'Main thread exiting.'        # don't exit too early

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.