Skip to content

Commit d0231d5

Browse files
committed
Make compatible with Python 2 and Python 3
1 parent 0962801 commit d0231d5

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

seleniumrequests/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from selenium.webdriver import Firefox, Chrome, Ie, Opera, Safari, PhantomJS, Android, Remote
22

3-
from request import RequestMixin
3+
from seleniumrequests.request import RequestMixin
44

55

66
class Firefox(Firefox, RequestMixin):

seleniumrequests/request.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import BaseHTTPServer
21
import socket
32
import threading
4-
import urlparse
53

4+
from six.moves import BaseHTTPServer
5+
from six.moves.urllib.parse import urlparse
66
import requests
7+
import six
78

89
from selenium.common.exceptions import NoSuchWindowException
910

11+
1012
_headers = None
1113
_update_headers_mutex = threading.Semaphore()
1214
_update_headers_mutex.acquire()
@@ -16,14 +18,19 @@
1618
# be the easiest way to get access to it, since the HTTPServer doesn't keep an
1719
# object of the instance of the _HTTPRequestHandler
1820
class _HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
21+
1922
def do_GET(self):
2023
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)
2229
_update_headers_mutex.release()
2330

2431
self.send_response(200)
2532
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>'))
2734

2835
# Suppress unwanted logging to stderr
2936
def log_message(*args, **kwargs):
@@ -66,6 +73,8 @@ def _get_webdriver_request_headers(webdriver):
6673
headers = _headers
6774
_headers = None
6875

76+
# Remove the Host-header which will simply contain the localhost address of
77+
# the _HTTPRequestHandler instance
6978
del headers['host']
7079
return headers
7180

@@ -75,7 +84,7 @@ def _prepare_requests_cookies(webdriver_cookies):
7584

7685

7786
def _get_domain(url):
78-
return '.'.join(urlparse.urlparse(url).netloc.rsplit('.', 2)[-2:])
87+
return '.'.join(urlparse(url).netloc.rsplit('.', 2)[-2:])
7988

8089

8190
def _find_window_handle(webdriver, callback):
@@ -112,6 +121,7 @@ def condition(webdriver):
112121

113122

114123
class RequestMixin(object):
124+
115125
def request(self, method, url, **kwargs):
116126
# Create a requests session object for this instance that sends the
117127
# webdriver's default request headers
@@ -141,7 +151,7 @@ def request(self, method, url, **kwargs):
141151

142152
# Create a new window handle manually in case it wasn't found
143153
if window_handle is None:
144-
components = urlparse.urlparse(url)
154+
components = urlparse(url)
145155
self.execute_script("window.open('http://%s');" % components.netloc)
146156
opened_window_handle = _find_window_handle(self, condition)
147157

0 commit comments

Comments
 (0)