Skip to content

Add "timeout" to "requests.get()" #2005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/boilerplates/samples/google_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
class GoogleTests(BaseCase):
def test_google_dot_com(self):
self.open("https://google.com/ncr")
self.sleep(0.5)
self.sleep(0.4)
self.save_screenshot_to_logs() # ("./latest_logs" folder)
self.sleep(0.5)
self.sleep(0.2)
self.type(HomePage.search_box, "github.com")
self.assert_element(HomePage.search_button)
self.assert_element(HomePage.feeling_lucky_button)
Expand Down
4 changes: 2 additions & 2 deletions examples/boilerplates/samples/test_page_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
class GooglePage:
def go_to_google(self, sb):
sb.open("https://google.com/ncr")
sb.sleep(0.2)
sb.hide_elements('iframe[name="callout"]') # Hide "Sign in" pop-up
sb.sleep(0.1)
sb.hide_elements('iframe') # Hide "Sign in" pop-up
sb.sleep(0.2)

def do_search(self, sb, search_term):
sb.click('[title="Search"]')
Expand Down
2 changes: 1 addition & 1 deletion examples/old_wordle_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WordleTests(BaseCase):

def initialize_word_list(self):
txt_file = "https://seleniumbase.github.io/cdn/txt/wordle_words.txt"
word_string = requests.get(txt_file).text
word_string = requests.get(txt_file, timeout=3).text
self.word_list = ast.literal_eval(word_string)

def modify_word_list(self, word, letter_status):
Expand Down
2 changes: 1 addition & 1 deletion examples/wordle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WordleTests(BaseCase):

def initialize_word_list(self):
txt_file = "https://seleniumbase.github.io/cdn/txt/wordle_words.txt"
word_string = requests.get(txt_file).text
word_string = requests.get(txt_file, timeout=3).text
self.word_list = ast.literal_eval(word_string)

def modify_word_list(self, word, letter_status):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pip>=23.2.1;python_version>="3.7"
packaging>=21.3;python_version<"3.7"
packaging>=23.1;python_version>="3.7"
setuptools>=59.6.0;python_version<"3.7"
setuptools>=68.0.0;python_version>="3.7"
setuptools>=68.0.0;python_version>="3.7" and python_version<"3.8"
setuptools>=68.1.0;python_version>="3.8"
wheel>=0.37.1;python_version<"3.7"
wheel>=0.41.1;python_version>="3.7"
attrs==22.1.0;python_version<"3.7"
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.17.5"
__version__ = "4.17.6"
64 changes: 32 additions & 32 deletions seleniumbase/console_scripts/sb_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,13 @@ def make_executable(file_path):
os.chmod(file_path, mode)


def requests_get(url):
response = None
try:
response = requests.get(url)
except Exception:
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
url = url.replace("https://", "http://")
response = requests.get(url)
return response


def requests_get_with_retry(url):
def get_proxy_info():
use_proxy = None
protocol = "http"
proxy_string = None
user_and_pass = None
if " --proxy=" in " ".join(sys.argv):
from seleniumbase.core import proxy_helper

for arg in sys.argv:
if arg.startswith("--proxy="):
proxy_string = arg.split("--proxy=")[1]
Expand All @@ -133,30 +122,41 @@ def requests_get_with_retry(url):
proxy_string = "%s@%s" % (user_and_pass, proxy_string)
use_proxy = True
break
return (use_proxy, protocol, proxy_string)


def requests_get(url):
use_proxy, protocol, proxy_string = get_proxy_info()
proxies = None
response = None
if use_proxy:
proxies = {protocol: proxy_string}
try:
response = requests.get(url, proxies=proxies, timeout=3)
except Exception:
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
url = url.replace("https://", "http://")
time.sleep(0.04)
response = requests.get(url, proxies=proxies, timeout=4)
return response


def requests_get_with_retry(url):
use_proxy, protocol, proxy_string = get_proxy_info()
proxies = None
response = None
if use_proxy:
proxies = {protocol: proxy_string}
try:
response = requests.get(url, proxies=proxies, timeout=3)
except Exception:
time.sleep(1)
try:
response = requests.get(url, proxies=proxies)
except Exception:
time.sleep(1.1)
try:
response = requests.get(url, proxies=proxies)
except Exception:
time.sleep(1.2)
response = requests.get(url, proxies=proxies)
return response
else:
try:
response = requests.get(url)
response = requests.get(url, proxies=proxies, timeout=4)
except Exception:
time.sleep(1.1)
try:
response = requests.get(url)
except Exception:
time.sleep(1.2)
response = requests.get(url)
return response
time.sleep(1)
response = requests.get(url, proxies=proxies, timeout=4)
return response


def main(override=None, intel_for_uc=None):
Expand Down
8 changes: 4 additions & 4 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ def requests_get(url, proxy_string=None):
if proxy_string:
proxies = {protocol: proxy_string}
try:
response = requests.get(url, proxies=proxies)
response = requests.get(url, proxies=proxies, timeout=3)
except Exception:
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
url = url.replace("https://", "http://")
time.sleep(0.04)
response = requests.get(url, proxies=proxies)
response = requests.get(url, proxies=proxies, timeout=4)
else:
try:
response = requests.get(url)
response = requests.get(url, timeout=3)
except Exception:
# Prevent SSLCertVerificationError / CERTIFICATE_VERIFY_FAILED
url = url.replace("https://", "http://")
time.sleep(0.04)
response = requests.get(url)
response = requests.get(url, timeout=4)
return response


Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/fixtures/js_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def add_css_style(driver, css_style):
def add_js_code_from_link(driver, js_link):
if js_link.startswith("//"):
js_link = "http:" + js_link
js_code = requests.get(js_link).text
js_code = requests.get(js_link, timeout=5).text
add_js_code_script = (
"""var body_tag=document.getElementsByTagName('body').item(0);"""
"""var script_tag=document.createElement("script");"""
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/fixtures/page_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
file_name = new_file_name
else:
file_name = file_url.split("/")[-1]
r = requests.get(file_url)
r = requests.get(file_url, timeout=5)
file_path = os.path.join(destination_folder, file_name)
download_file_lock = fasteners.InterProcessLock(
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/undetected/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__(

special_port_free = False # If the port isn't free, don't use 9222
try:
res = requests.get("http://127.0.0.1:9222")
res = requests.get("http://127.0.0.1:9222", timeout=1)
if res.status_code != 200:
raise Exception("The port is free! It will be used!")
except Exception:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
'packaging>=21.3;python_version<"3.7"',
'packaging>=23.1;python_version>="3.7"',
'setuptools>=59.6.0;python_version<"3.7"',
'setuptools>=68.0.0;python_version>="3.7"',
'setuptools>=68.0.0;python_version>="3.7" and python_version<"3.8"',
'setuptools>=68.1.0;python_version>="3.8"',
'wheel>=0.37.1;python_version<"3.7"',
'wheel>=0.41.1;python_version>="3.7"',
'attrs==22.1.0;python_version<"3.7"',
Expand Down