wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 254 forks source link

502 - Bad Gateway #78

Closed tusharacc closed 3 years ago

tusharacc commented 4 years ago

I am trying to use selenium-wire to monitor the requests. Basically, my ask is to monitor the network traffic to ensure no more calls are being made, so that I can get on with other task at hand. However I am getting 502 error. I was able to use the same script without selenium-wire.

Error response Error code: 502

Message: Bad Gateway.

Error code explanation: 502 - Invalid responses from another server/proxy.

Please note, I am behind a VPN. However when I disconnect VPN, I still get the same error

I have used the inputs from other similar posts.

Please see the log -

INFO:seleniumwire.proxy.client:Created proxy listening on 127.0.0.1:50735 DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:50736/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "proxy": {"proxyType": "manual", "httpProxy": "127.0.0.1:50735", "sslProxy": "127.0.0.1:50735", "noProxy": ""}, "acceptInsecureCerts": true}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "proxy": {"proxyType": "manual", "httpProxy": "127.0.0.1:50735", "sslProxy": "127.0.0.1:50735", "noProxy": ""}, "acceptInsecureCerts": true}} DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1

DevTools listening on ws://127.0.0.1:12229/devtools/browser/c6de2460-ec93-479d-bf38-044954bb5659 DEBUG:seleniumwire.proxy.handler:accounts.google.com:443 200 INFO:seleniumwire.proxy.handler:Capturing request: https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway Traceback (most recent call last): File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 106, in do_GET conn = self.create_connection(origin) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 161, in create_connection self.tls.conns[origin] = connection(proxyconfig, netloc, **kwargs) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 279, in init , proxy_username, proxy_password, proxy_host = proxy_config.get('https') TypeError: 'NoneType' object is not iterable DEBUG:seleniumwire.proxy.handler:https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard 502 DEBUG:urllib3.connectionpool:http://127.0.0.1:50736 "POST /session HTTP/1.1" 200 828 DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:50736/session/984da091f57ea28a41cb750c3d629c1a/url {"url": "http://google.com", "sessionId": "984da091f57ea28a41cb750c3d629c1a"} INFO:seleniumwire.proxy.handler:Capturing request: http://google.com/ DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway Traceback (most recent call last): File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 106, in do_GET conn = self.create_connection(origin) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 161, in create_connection self.tls.conns[origin] = connection(proxyconfig, netloc, **kwargs) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 252, in init , self.proxy_username, self.proxy_password, self.proxy_host = proxy_config.get('http') TypeError: 'NoneType' object is not iterable DEBUG:seleniumwire.proxy.handler:http://google.com/ 502 INFO:seleniumwire.proxy.handler:Capturing request: http://google.com/favicon.ico DEBUG:urllib3.connectionpool:http://127.0.0.1:50736 "POST /session/984da091f57ea28a41cb750c3d629c1a/url HTTP/1.1" 200 72 DEBUG:selenium.webdriver.remote.remote_connection:Finished Request DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway Traceback (most recent call last): File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 106, in do_GET conn = self.create_connection(origin) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 161, in create_connection self.tls.conns[origin] = connection(proxyconfig, netloc, **kwargs) File "C:\Users\t9sau2\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 252, in init , self.proxy_username, self.proxy_password, self.proxy_host = proxy_config.get('http') TypeError: 'NoneType' object is not iterable DEBUG:seleniumwire.proxy.handler:http://google.com/favicon.ico 502 DEBUG:seleniumwire.proxy.handler:http://seleniumwire/requests 200 DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:50736/session/984da091f57ea28a41cb750c3d629c1a/element {"using": "id", "value": "username", "sessionId": "984da091f57ea28a41cb750c3d629c1a"} DEBUG:seleniumwire.proxy.handler:accounts.google.com:443 200 DEBUG:urllib3.connectionpool:http://127.0.0.1:50736 "POST /session/984da091f57ea28a41cb750c3d629c1a/element HTTP/1.1" 200 335 DEBUG:selenium.webdriver.remote.remote_connection:Finished Request

The code is below

from seleniumwire import webdriver options = { 'suppress_connection_errors': False , 'connection_timeout': None, # Never timeout 'verify_ssl': False } driver = webdriver.Chrome(seleniumwire_options=options) driver.get('http://google.com')

wkeeling commented 4 years ago

Thank you for raising this and sorry for the delay in replying.

I think the clue is here:

_, self.proxy_username, self.proxy_password, self.proxy_host = proxy_config.get('http')
TypeError: 'NoneType' object is not iterable

This suggests that there is some empty proxy config specified somewhere - although your example code indicates that you're not using a proxy. Can you double-check that you haven't got any proxy configuration specified in your code? e.g.

options = {
    'proxy': {}
}
shayegh commented 4 years ago

Hi, I got the same error. I also used @wkeeling suggestion but no success please help me.

import logging

logging.basicConfig(level=logging.DEBUG)

from seleniumwire import webdriver  # Import from seleniumwire

options = { 'suppress_connection_errors': False , 'connection_timeout': None,'verify_ssl': False,'proxy': {} }
# Create a new instance of the Chrome driver
driver = webdriver.Chrome(seleniumwire_options=options)

# Go to the Google home page
driver.get('https://www.google.com')

# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
        print(
            request.path,
            request.response.status_code,
            request.response.headers['Content-Type']
        )

Result:

INFO:seleniumwire.proxy.client:Created proxy listening on 127.0.0.1:57548
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57549/session {"capabilities": {"firstMatch": [{}], "alwaysMatch": {"browserName": "chrome", "platformName": "any", "proxy": {"proxyType": "manual", "httpProxy": "127.0.0.1:57548", "sslProxy": "127.0.0.1:57548", "noProxy": ""}, "acceptInsecureCerts": true}}, "desiredCapabilities": {"browserName": "chrome", "version": "", "platform": "ANY", "proxy": {"proxyType": "manual", "httpProxy": "127.0.0.1:57548", "sslProxy": "127.0.0.1:57548", "noProxy": ""}, "acceptInsecureCerts": true}}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 127.0.0.1:57549
DEBUG:seleniumwire.proxy.handler:accounts.google.com:443 200
INFO:seleniumwire.proxy.handler:Capturing request: https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard
DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 107, in do_GET
    conn = self.create_connection(origin)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 163, in create_connection
    self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 281, in __init__
    _, proxy_username, proxy_password, proxy_host = proxy_config.get('https')
TypeError: cannot unpack non-iterable NoneType object
DEBUG:seleniumwire.proxy.handler:https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard 502
DEBUG:seleniumwire.proxy.handler:accounts.google.com:443 200
INFO:seleniumwire.proxy.handler:Capturing request: https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard
DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 107, in do_GET
    conn = self.create_connection(origin)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 163, in create_connection
    self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 281, in __init__
    _, proxy_username, proxy_password, proxy_host = proxy_config.get('https')
TypeError: cannot unpack non-iterable NoneType object
DEBUG:seleniumwire.proxy.handler:https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard 502
DEBUG:seleniumwire.proxy.handler:accounts.google.com:443 200
INFO:seleniumwire.proxy.handler:Capturing request: https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard
DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 107, in do_GET
    conn = self.create_connection(origin)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 163, in create_connection
    self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 281, in __init__
    _, proxy_username, proxy_password, proxy_host = proxy_config.get('https')
TypeError: cannot unpack non-iterable NoneType object
DEBUG:seleniumwire.proxy.handler:https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard 502
DEBUG:urllib3.connectionpool:http://127.0.0.1:57549 "POST /session HTTP/1.1" 200 807
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:57549/session/6a039feac25af0eb3b163e6d6a7e5dcc/url {"url": "https://www.google.com"}
DEBUG:seleniumwire.proxy.handler:www.google.com:443 200
DEBUG:seleniumwire.proxy.handler:www.google.com:443 200
INFO:seleniumwire.proxy.handler:Capturing request: https://www.google.com/
DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 107, in do_GET
    conn = self.create_connection(origin)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 163, in create_connection
    self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 281, in __init__
    _, proxy_username, proxy_password, proxy_host = proxy_config.get('https')
TypeError: cannot unpack non-iterable NoneType object
DEBUG:seleniumwire.proxy.handler:https://www.google.com/ 502
DEBUG:urllib3.connectionpool:http://127.0.0.1:57549 "POST /session/6a039feac25af0eb3b163e6d6a7e5dcc/url HTTP/1.1" 200 14
INFO:seleniumwire.proxy.handler:Capturing request: https://www.google.com/favicon.ico
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:seleniumwire.proxy.handler:code 502, message Bad Gateway
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 107, in do_GET
    conn = self.create_connection(origin)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 163, in create_connection
    self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\seleniumwire\proxy\proxy2.py", line 281, in __init__
    _, proxy_username, proxy_password, proxy_host = proxy_config.get('https')
TypeError: cannot unpack non-iterable NoneType object
DEBUG:seleniumwire.proxy.handler:https://www.google.com/favicon.ico 502
DEBUG:seleniumwire.proxy.handler:http://seleniumwire/requests 200
demisx commented 4 years ago

Any idea where this "https://accounts.google.com/ListAccounts?gpsia=1&..." URL comes from? Who sets it? I am seeing the same error in my logs.

shayegh commented 4 years ago

No, I try to activate this URL "https://d.easytrader.emofid.com/" But I'm not its developer

wkeeling commented 4 years ago

@shayegh Can you remove 'proxy': {} from your options and retry? Your options should look like:

options = {
    'suppress_connection_errors': False, 
    'connection_timeout': None, 
    'verify_ssl': False
}
wkeeling commented 4 years ago

@shayegh did you manage to resolve this issue?

Rexexe commented 3 years ago

Unfortunately, So did I

Error making request Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 92, in proxy_request conn.request(self.command, path, req_body, dict(req.headers)) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1252, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1298, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1247, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 966, in send self.connect() File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\seleniumwire\proxy\proxy2.py", line 369, in connect super().connect() File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1414, in connect super().connect() File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 938, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 728, in create_connection raise err File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 716, in create_connection sock.connect(sa) TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

http://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard