Advertisement
Guest User

python nettest

a guest
Jan 25th, 2014
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import timeit
  2. import socket
  3. import StringIO
  4. import urllib
  5. import urllib2
  6. import httplib
  7. import requests
  8. import pycurl
  9. from urlparse import urlparse
  10.  
  11. uagent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'
  12. testUrl = 'http://vk.com/'
  13. okPattern = 'scroll_fix'
  14.  
  15. maxIterations = 100
  16. socketTimeout = 5
  17.  
  18. socket.setdefaulttimeout(socketTimeout)
  19.  
  20. def testUrllib2():
  21.  
  22.    counter = 0
  23.    opener = urllib2.build_opener()
  24.    opener.addheaders = [('User-agent', uagent)]
  25.  
  26.    for loopCount in range(0, maxIterations):
  27.       pageContent = opener.open(testUrl)
  28.       html = pageContent.read()
  29.  
  30.       if okPattern in html:
  31.          counter +=1
  32.  
  33.    print counter
  34.  
  35. def testUrllib():
  36.  
  37.    counter = 0
  38.    class myURLopener(urllib.FancyURLopener, object):
  39.       version = uagent
  40.  
  41.    opener = myURLopener()
  42.  
  43.    for loopCount in range(0, maxIterations):
  44.  
  45.       pageContent = opener.open(testUrl)
  46.       html = pageContent.read()
  47.  
  48.       if okPattern in html:
  49.          counter +=1
  50.  
  51.    print counter
  52.  
  53.  
  54. def testHttplib():
  55.  
  56.    counter = 0
  57.    headers = {'User-Agent': uagent}
  58.    myUrl = urlparse(testUrl)
  59.    conn = httplib.HTTPConnection(myUrl.netloc)
  60.  
  61.    for loopCount in range(0, maxIterations):
  62.  
  63.       conn.request("GET", myUrl.path, '', headers)
  64.       pageContent = conn.getresponse()
  65.       html = pageContent.read()
  66.  
  67.       if okPattern in html:
  68.          counter +=1
  69.  
  70.    print counter
  71.  
  72.  
  73. def testSocket():
  74.  
  75.    counter = 0
  76.    myUrl = urlparse(testUrl)
  77.  
  78.    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  79.    sock.connect((myUrl.netloc, 80))
  80.  
  81.    for loopCount in range(0, maxIterations):  
  82.  
  83.       sock.send("GET %s HTTP/1.1\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: Keep-Alive\r\n\r\n" % (myUrl.path, myUrl.netloc, uagent))
  84.       html = sock.recv(65535)
  85.  
  86.       if okPattern in html:
  87.          counter +=1
  88.  
  89.    sock.close()
  90.    print counter
  91.  
  92. def testRequests():
  93.  
  94.    counter = 0
  95.    headers = {'User-Agent': uagent}
  96.  
  97.    for loopCount in range(0, maxIterations):
  98.  
  99.       r = requests.get(testUrl, headers=headers, stream=True, timeout=5)
  100.       html = r.text
  101.  
  102.       if okPattern in html:
  103.          counter +=1
  104.  
  105.    print counter
  106.  
  107.  
  108. def testPycurl():
  109.  
  110.    counter = 0
  111.    html = StringIO.StringIO()
  112.  
  113.  
  114.    curlHandler = pycurl.Curl()
  115.    curlHandler.setopt(pycurl.USERAGENT, uagent)
  116.    curlHandler.setopt(pycurl.WRITEFUNCTION, html.write)
  117.  
  118.  
  119.    for loopCount in range(0, maxIterations):
  120.       curlHandler.setopt(pycurl.URL, testUrl)
  121.       curlHandler.perform()
  122.  
  123.       if okPattern in html.getvalue():
  124.          counter +=1
  125.  
  126.    print counter
  127.  
  128.  
  129.  
  130.  
  131. method = "testUrllib()"
  132. print(method, timeit.timeit(method, setup="from __main__ import testUrllib", number=1))
  133.  
  134. method = "testUrllib2()"
  135. print(method, timeit.timeit(method, setup="from __main__ import testUrllib2", number=1))
  136.  
  137. method = "testHttplib()"
  138. print(method, timeit.timeit(method, setup="from __main__ import testHttplib", number=1))
  139.  
  140. method = "testSocket()"
  141. print(method, timeit.timeit(method, setup="from __main__ import testSocket", number=1))
  142.  
  143. method = "testRequests()"
  144. print(method, timeit.timeit(method, setup="from __main__ import testRequests", number=1))
  145.  
  146. method = "testPycurl()"
  147. print(method, timeit.timeit(method, setup="from __main__ import testPycurl", number=1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement