小编Clo*_*own的帖子

无法使用Python通过socks5代理发送请求

我试图通过代理(socks5)发送http/https 请求,但我无法理解问题是否出在我的代码中或代理中。

我尝试使用这段代码,但它给了我一个错误:

requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='www.google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x000001B656AC9608>: Failed to establish a new connection: Connection closed unexpectedly'))
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import requests

url = "https://www.google.com"


proxies = {
    "http":"socks5://fsagsa:sacesf241_country-darwedafs_session-421dsafsa@x.xxx.xxx.xx:31112",
    "https":"socks5://fsagsa:sacesf241_country-darwedafs_session-421dsafsa@x.xxx.xxx.xx:31112",
    }

headers = {
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
        "Sec-Gpc": "1",
        "Sec-Fetch-Site": "same-origin",
        "Sec-Fetch-User": "?1",
        "Accept-Encoding": "gzip, deflate, br",
        "Sec-Fetch-Mode": "navigate",
        "Sec-Fetch-Dest": "document",
        "Accept-Language": "en-GB,en;q=0.9"
    }


r = …
Run Code Online (Sandbox Code Playgroud)

python proxy socks python-requests

4
推荐指数
2
解决办法
6269
查看次数

time.sleep() 和迭代直到秒过去之间的差异

我想知道time.sleep(5)下面这段代码之间有什么区别:

import time

start_time = time.time()
while True:
    if time.time() - start_time > 5:
        break
print("Five seconds passed")
Run Code Online (Sandbox Code Playgroud)

而且,使用第一种或第二种解决方案更好吗?可能,while像我一样使用循环并不是一个好的解决方案。

谢谢指教。

python time sleep python-3.x

0
推荐指数
1
解决办法
32
查看次数

标签 统计

python ×2

proxy ×1

python-3.x ×1

python-requests ×1

sleep ×1

socks ×1

time ×1