Forums

Help proxy error

Hi,

I am a newbie in python.

I coded a small script, which collects data from an API weblink. It runs smoothy on my PC with PyCharm 2021.3.2, but on python anywhere I receive the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 700, in urlopen
    self._prepare_proxy(conn)
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 994, in _prepare_proxy
    conn.connect()
  File "/usr/local/lib/python3.10/site-packages/urllib3/connection.py", line 369, in connect
    self._tunnel()
  File "/usr/local/lib/python3.10/http/client.py", line 924, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 403 Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.marketinout.com', port=443): Max retries exceeded with url: /run/screen?key=4d5b61ddddec4e4d (Caused 
by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/Monacocode/main.py", line 18, in <module>
    response = requests.get(url)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 559, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.marketinout.com', port=443): Max retries exceeded with url: /run/screen?key=4d5b61ddddec4e4d (Caused by
 ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

Can anybody help me?

Here is the new code, but the error is now:

Proxy Error: HTTPSConnectionPool(host='api.marketinout.com', port=443): Max retries exceeded with url: /run/screen?key=91c882e09375463a (Caused by ProxyError('Cannot
 connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f76616aee00>: Failed to establish a new connection: [Errno 111] Connection 
refused')))

code:

import requests
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

url = "https://api.marketinout.com/run/screen?key=XXXXXXXXXXX"

# Configure email settings
sender_email = "XX"
receiver_email = "XX"
smtp_server = "XX"
smtp_port = XX
smtp_username = "XX"
smtp_password = "XX"

# Set proxy settings (modify according to your environment)
proxies = {
      'https': 'https://your_proxy_here',
}

while True:
    try:
        # Send a GET request to the URL with proxy settings
        response = requests.get(url, proxies=proxies)

        # Check if the request was successful
        if response.status_code == 200:
            # Count the number of lines in the response text
            line_count = response.text.count('\n')

            print("Number of lines:", line_count)

            # Check if the line count exceeds 5
            if line_count > 5:
                # Compose the email message
                subject = "Line Count"
                body = f"The current line count is: {line_count}"

                message = MIMEMultipart()
                message["From"] = sender_email
                message["To"] = receiver_email
                message["Subject"] = subject

                message.attach(MIMEText(body, "plain"))

                # Create a secure connection to the SMTP server with proxy settings
                with smtplib.SMTP(smtp_server, smtp_port) as server:
                    server.starttls()
                    server.login(smtp_username, smtp_password)
                    server.sendmail(sender_email, receiver_email, message.as_string())

            # Rest of your code...

        else:
            print("Failed to fetch the webpage. Status code:", response.status_code)

    except requests.exceptions.ProxyError as pe:
        print(f"Proxy Error: {pe}")
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")

    # Wait for 5 minutes before refreshing the link
    time.sleep(300)

[edit by admin: formatting]

[edit by admin: api key removal]

Hi there, looks like you're running into the api whitelist for free users. You can request additions here -> https://help.pythonanywhere.com/pages/RequestingAllowlistAdditions

FYI I removed what looked like an api key from the api url you posted ("https://api.marketinout.com/run/screen?key=XXXXXXXXXXX"), you'll want to reissue a new key if it was supposed to be private

I think you can remove the bit of code about proxies unless you actually have a proxy set up

Thanks for your help. The question is, why does run on my desktop pc and not on python anywhere?

Internet access on PythonAnywhere is restricted to sites on our allowlist for free users. Check the help pages that Sam linked above.

Thanks. I upgraded to premium and now it works. I am not sure how, if I consume more CPU-time than I booked if I have to pay more or if the script will not be executed anymore.

If you use up more CPU time than your quota, we'll send you an email (assuming you haven't unsubscribed from resource warning emails on the "Account" page) and we'll continue to run your script, but at a lower priority, so it will slow down. If you manage to keep running and use 10x your allowance in a day, then your script may be terminated.

So if you get the warning email, you might want to consider upgrading to a custom plan with more CPU.

.

File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/requests/adapters.py", line 565, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='tenor.com', port=443): Max retries exceeded with url: /ru/search/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%8
2-gifs (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fe9a5313f40>: Failed to establish a new connection: [Errno 111] Connection refused'))

почему код не работает на хостинге а на моём компе работает норм

[edit by admin: formatting]

Free accounts on PythonAnywhere have restricted Internet access; they have to use our proxy (available to you via the URL you get if you run echo $http_proxy in Bash), and they can only access sites on our allowlist. From that stack trace you've provided, it looks like you're trying to connect via a different remote proxy, and you won't be able to do that in a free account.

Can you add access to Readwise as documented here?

You'll need to use the proper request form - https://help.pythonanywhere.com/pages/RequestingAllowlistAdditions/