1- #!/usr/bin/python2.6
1+ #!/usr/bin/python3
22#
33# Copyright 2011 Google Inc. All Rights Reserved.
44
1313
1414__author__ = '[email protected] (Qi Zhao)' 1515
16- import re
17- import urllib
16+ import urllib . request
17+ import urllib . parse
1818from xml .dom import minidom
1919
2020
21- def __LoadEntity (url , urlopen = urllib .urlopen ):
21+ def __LoadEntity (url , apiKey , urlopen = urllib . request .urlopen ):
2222 """A helper function to load an entity such as an URL.
2323
2424 Args:
@@ -28,7 +28,15 @@ def __LoadEntity(url, urlopen=urllib.urlopen):
2828 Returns:
2929 The response message
3030 """
31- response = urlopen (url )
31+ headers = {}
32+
33+ if apiKey :
34+ headers = {
35+ "X-WPT-API-KEY" : apiKey
36+ }
37+
38+ request = urllib .request .Request (url , headers = headers )
39+ response = urlopen (request )
3240 return response
3341
3442
@@ -44,14 +52,14 @@ def ImportUrls(url_filename):
4452 url_list = []
4553 for line in open (url_filename , 'rb' ):
4654 # Remove newline and trailing whitespaces
47- url = line .rstrip (' \r \n ' )
55+ url = line .rstrip (b ' \r \n ' )
4856 if url :
4957 url_list .append (url )
5058 return url_list
5159
5260
53- def SubmitBatch (url_list , test_params , server_url = 'http://www.webpagetest.org/' ,
54- urlopen = urllib .urlopen ):
61+ def SubmitBatch (url_list , test_params , apiKey , server_url = 'http://www.webpagetest.org/' ,
62+ urlopen = urllib .request . urlopen , ):
5563 """Submit the tests to WebPageTest server.
5664
5765 Args:
@@ -67,8 +75,8 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
6775 id_url_dict = {}
6876 for url in url_list :
6977 test_params ['url' ] = url
70- request = server_url + 'runtest.php?%s' % urllib .urlencode (test_params )
71- response = __LoadEntity (request , urlopen )
78+ request = server_url + 'runtest.php?%s' % urllib .parse . urlencode (test_params )
79+ response = __LoadEntity (request , apiKey , urlopen )
7280 return_code = response .getcode ()
7381 if return_code == 200 :
7482 dom = minidom .parseString (response .read ())
@@ -81,7 +89,7 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
8189
8290
8391def CheckBatchStatus (test_ids , server_url = 'http://www.webpagetest.org/' ,
84- urlopen = urllib .urlopen ):
92+ urlopen = urllib .request . urlopen ):
8593 """Check the status of tests.
8694
8795 Args:
@@ -105,7 +113,7 @@ def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
105113
106114
107115def GetXMLResult (test_ids , server_url = 'http://www.webpagetest.org/' ,
108- urlopen = urllib .urlopen ):
116+ urlopen = urllib .request . urlopen ):
109117 """Obtain the test result in XML format.
110118
111119 Args:
0 commit comments