The error message you're encountering:
No internet
There is something wrong with the proxy server, or the address is incorrect.
Try:
- Contacting the system admin
- Checking the proxy address
- ERR_PROXY_CONNECTION_FAILED
indicates that the application is unable to connect to the internet through the specified proxy server. Let's break down the potential causes and provide solutions to resolve this issue.
Possible Causes
-
Proxy Server Not Running or Misconfigured:
- The SOCKS5 proxy server (
127.0.0.1:7891) might not be active or properly configured.
-
Incorrect Proxy Address or Port:
- The IP address or port number specified for the proxy might be incorrect.
-
Incorrect Proxy Configuration in PyQt6:
- The method used to set proxy settings in your PyQt6 application might not be effective for
QWebEngineView.
-
Firewall or Network Restrictions:
- Local firewall settings or network policies might be blocking the proxy connection.
Step-by-Step Solutions
1. Verify the Proxy Server
Before diving into the application code, ensure that the proxy server is up and running.
2. Validate Proxy Address and Port
Ensure that the proxy address (127.0.0.1) and port (7891) are correct.
- Common Mistakes:
- Typographical errors in the IP address or port.
- The proxy server is bound to a different IP (e.g.,
0.0.0.0) or a different port.
3. Configure Proxy Settings Correctly in PyQt6
The method used to set proxy settings via environment variables (http_proxy and https_proxy) is not effective for QWebEngineView. Instead, you need to set proxy settings using Chromium flags, as QWebEngineView is based on the Chromium engine.
Here's how to properly configure the proxy:
Updated Code Example
import sys
import os
from PyQt6.QtCore import QUrl
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtWebEngineWidgets import QWebEngineView
class Browser(QMainWindow):
def __init__(self):
super().__init__()
# Create QWebEngineView object
self.browser = QWebEngineView()
# Set initial page URL
self.browser.setUrl(QUrl("https://www.qt.io"))
# Set the browser as the central widget
self.setCentralWidget(self.browser)
# Set window title and size
self.setWindowTitle("PyQt6 Web Browser")
self.resize(1024, 768)
if __name__ == "__main__":
# Configure Chromium proxy settings
proxy = "socks5://127.0.0.1:7891" # Replace with your actual proxy address and port
os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = f'--proxy-server={proxy}'
app = QApplication(sys.argv)
# Optionally, pass the proxy as a command-line argument
# app.setAttribute(QtCore.Qt.ApplicationAttribute., value)
# Create browser window
window = Browser()
window.show()
# Enter the application's main loop
sys.exit(app.exec())
Explanation of Changes
-
Set QTWEBENGINE_CHROMIUM_FLAGS:
- Use the
QTWEBENGINE_CHROMIUM_FLAGS environment variable to pass Chromium-specific flags.
- This method ensures that
QWebEngineView uses the specified proxy settings.
os.environ['QTWEBENGINE_CHROMIUM_FLAGS'] = f'--proxy-server={proxy}'
-
Remove http_proxy and https_proxy Environment Variables:
- These are not effective for
QWebEngineView and can be removed to avoid confusion.
-
Ensure Proper Order:
- Set the environment variable before initializing the
QApplication to ensure the settings take effect.
4. Additional Proxy Configuration (Optional)
If your proxy requires authentication or uses a different protocol, you might need to adjust the proxy string accordingly.
5. Check Firewall and Network Settings
Ensure that your local firewall or network settings are not blocking the proxy connection.
-
Firewall Rules:
- Allow inbound and outbound connections on port
7891.
-
Network Policies:
- Some corporate or restricted networks may block proxy connections. Verify with your network administrator if necessary.
6. Test the Proxy Independently
Before integrating with PyQt6, test the proxy using a web browser or command-line tool to ensure it's functioning correctly.
7. Debugging Tips
Summary
Your current approach of setting http_proxy and https_proxy environment variables doesn't affect QWebEngineView. Instead, you should set the proxy using the QTWEBENGINE_CHROMIUM_FLAGS environment variable with the --proxy-server flag. Additionally, ensure that your proxy server is running correctly and that the address and port are accurate.
By following the updated code example and the steps outlined above, you should be able to resolve the ERR_PROXY_CONNECTION_FAILED error and have your PyQt6-based browser connect successfully through the specified proxy server.