1
- import BaseHTTPServer
2
1
import socket
3
2
import threading
4
- import urlparse
5
3
4
+ from six .moves import BaseHTTPServer
5
+ from six .moves .urllib .parse import urlparse
6
6
import requests
7
+ import six
7
8
8
9
from selenium .common .exceptions import NoSuchWindowException
9
10
11
+
10
12
_headers = None
11
13
_update_headers_mutex = threading .Semaphore ()
12
14
_update_headers_mutex .acquire ()
16
18
# be the easiest way to get access to it, since the HTTPServer doesn't keep an
17
19
# object of the instance of the _HTTPRequestHandler
18
20
class _HTTPRequestHandler (BaseHTTPServer .BaseHTTPRequestHandler ):
21
+
19
22
def do_GET (self ):
20
23
global _headers
21
- _headers = self .headers .dict
24
+
25
+ # Python 2's HTTPMessage class contains the actual data in its
26
+ # "dict"-attribute, whereas in Python 3 HTTPMessage is itself the
27
+ # container. Treat headers as case-insensitive
28
+ _headers = requests .structures .CaseInsensitiveDict (self .headers if six .PY3 else self .headers .dict )
22
29
_update_headers_mutex .release ()
23
30
24
31
self .send_response (200 )
25
32
self .end_headers ()
26
- self .wfile .write ('<script type="text/javascript">window.close();</script>' )
33
+ self .wfile .write (six . b ( '<script type="text/javascript">window.close();</script>' ) )
27
34
28
35
# Suppress unwanted logging to stderr
29
36
def log_message (* args , ** kwargs ):
@@ -66,6 +73,8 @@ def _get_webdriver_request_headers(webdriver):
66
73
headers = _headers
67
74
_headers = None
68
75
76
+ # Remove the Host-header which will simply contain the localhost address of
77
+ # the _HTTPRequestHandler instance
69
78
del headers ['host' ]
70
79
return headers
71
80
@@ -75,7 +84,7 @@ def _prepare_requests_cookies(webdriver_cookies):
75
84
76
85
77
86
def _get_domain (url ):
78
- return '.' .join (urlparse . urlparse (url ).netloc .rsplit ('.' , 2 )[- 2 :])
87
+ return '.' .join (urlparse (url ).netloc .rsplit ('.' , 2 )[- 2 :])
79
88
80
89
81
90
def _find_window_handle (webdriver , callback ):
@@ -112,6 +121,7 @@ def condition(webdriver):
112
121
113
122
114
123
class RequestMixin (object ):
124
+
115
125
def request (self , method , url , ** kwargs ):
116
126
# Create a requests session object for this instance that sends the
117
127
# webdriver's default request headers
@@ -141,7 +151,7 @@ def request(self, method, url, **kwargs):
141
151
142
152
# Create a new window handle manually in case it wasn't found
143
153
if window_handle is None :
144
- components = urlparse . urlparse (url )
154
+ components = urlparse (url )
145
155
self .execute_script ("window.open('http://%s');" % components .netloc )
146
156
opened_window_handle = _find_window_handle (self , condition )
147
157
0 commit comments