1
- #from httplib2 import Http
2
1
import requests
3
2
from urllib import urlencode
4
3
import json
5
4
5
+
6
6
class Client (object ):
7
-
8
7
def __init__ (self , url ):
9
8
"""
10
9
Initialises a new Client object
11
10
:Args:
12
11
- url: This is where the BrowserMob Proxy lives
13
12
"""
14
- self .host = url
13
+ self .host = url
15
14
resp = requests .post ('%s/proxy' % self .host , urlencode ('' ))
16
15
jcontent = json .loads (resp .content )
17
16
self .port = jcontent ['port' ]
18
17
url_parts = self .host .split (":" )
19
- self .proxy = url_parts [0 ] + ":" + url_parts [1 ] + ":" + str (self .port )
18
+ self .proxy = url_parts [0 ] + ":" + url_parts [1 ] + ":" + str (self .port )
20
19
21
20
def headers (self , headers ):
22
21
"""
@@ -29,7 +28,7 @@ def headers(self, headers):
29
28
30
29
r = requests .post (url = '%s/proxy/%s/headers' % (self .host , self .port ),
31
30
data = json .dumps (headers ),
32
- headers = {'content-type' : 'application/json' })
31
+ headers = {'content-type' : 'application/json' })
33
32
return r .status_code
34
33
35
34
def new_har (self , ref = None ):
@@ -55,24 +54,25 @@ def new_page(self, ref=None):
55
54
payload = {"pageRef" : ref }
56
55
else :
57
56
payload = {}
58
- r = requests .put ('%s/proxy/%s/har/pageRef' % (self .host , self .port ), payload )
57
+ r = requests .put ('%s/proxy/%s/har/pageRef' % (self .host , self .port ),
58
+ payload )
59
59
return r .status_code
60
-
60
+
61
61
@property
62
62
def har (self ):
63
63
"""
64
64
Gets the HAR that has been recorded
65
65
"""
66
66
r = requests .get ('%s/proxy/%s/har' % (self .host , self .port ))
67
-
67
+
68
68
return r .json
69
69
70
70
def selenium_proxy (self ):
71
71
"""
72
72
Returns a Selenium WebDriver Proxy class with details of the HTTP Proxy
73
73
"""
74
74
from selenium import webdriver
75
- return webdriver .Proxy ({"httpProxy" :self .proxy })
75
+ return webdriver .Proxy ({"httpProxy" : self .proxy })
76
76
77
77
def webdriver_proxy (self ):
78
78
"""
@@ -82,7 +82,8 @@ def webdriver_proxy(self):
82
82
83
83
def add_to_webdriver_capabilities (self , capabilities ):
84
84
"""
85
- Adds an 'proxy' entry to a desired capabilities dictionary with the BrowserMob proxy information
85
+ Adds an 'proxy' entry to a desired capabilities dictionary with the
86
+ BrowserMob proxy information
86
87
"""
87
88
capabilities ['proxy' ] = {'proxyType' : 'manual' ,
88
89
'httpProxy' : self .proxy }
@@ -92,34 +93,31 @@ def whitelist(self, regexp, status_code):
92
93
Sets a list of URL patterns to whitelist
93
94
:Args:
94
95
- regex: a comma separated list of regular expressions
95
- - status_code: the HTTP status code to return for URLs that do not match the whitelist
96
-
96
+ - status_code: the HTTP status code to return for URLs that do not
97
+ match the whitelist
97
98
"""
98
- r = requests .put ('%s/proxy/%s/whitelist' % (self .host , self .port ),
99
- urlencode ({ 'regex' : regexp , 'status' : status_code
100
- }))
99
+ r = requests .put ('%s/proxy/%s/whitelist' % (self .host , self .port ),
100
+ urlencode ({'regex' : regexp , 'status' : status_code }))
101
101
return r .status_code
102
102
103
-
104
103
def blacklist (self , regexp , status_code ):
105
104
"""
106
- Sets a list of URL patterns to blacklist
105
+ Sets a list of URL patterns to blacklist
107
106
:Args:
108
107
- regex: a comma separated list of regular expressions
109
- - status_code: the HTTP status code to return for URLs that do not match the blacklist
108
+ - status_code: the HTTP status code to return for URLs that do not
109
+ match the blacklist
110
110
111
111
"""
112
- r = requests .put ('%s/proxy/%s/blacklist' % (self .host , self .port ),
113
- urlencode ({ 'regex' : regexp , 'status' : status_code
114
- }))
112
+ r = requests .put ('%s/proxy/%s/blacklist' % (self .host , self .port ),
113
+ urlencode ({'regex' : regexp , 'status' : status_code }))
115
114
return r .status_code
116
115
117
116
LIMITS = {
118
- 'upstream_kbps' : 'upstreamKbps' ,
119
- 'downstream_kbps' : 'downstreamKbps' ,
120
- 'latency' : 'latency'
121
- }
122
-
117
+ 'upstream_kbps' : 'upstreamKbps' ,
118
+ 'downstream_kbps' : 'downstreamKbps' ,
119
+ 'latency' : 'latency'
120
+ }
123
121
124
122
def limits (self , options ):
125
123
"""
@@ -133,16 +131,16 @@ def limits(self, options):
133
131
params = {}
134
132
135
133
for (k , v ) in options .items ():
136
- if not self .LIMITS . has_key ( k ) :
134
+ if k not in self .LIMITS :
137
135
raise KeyError ('invalid key: %s' % k )
138
136
139
137
params [self .LIMITS [k ]] = int (v )
140
138
141
139
if len (params .items ()) == 0 :
142
140
raise KeyError ("You need to specify one of the valid Keys" )
143
-
144
- r = requests .put ('%s/proxy/%s/limit' % (self .host , self .port ),
145
- urlencode (params ))
141
+
142
+ r = requests .put ('%s/proxy/%s/limit' % (self .host , self .port ),
143
+ urlencode (params ))
146
144
return r .status_code
147
145
148
146
def close (self ):
0 commit comments