Forums

Error code: 502-backend

Something went wrong :-( Something went wrong while trying to load this site; please try again later.

Debugging tips If this is your site, and you just reloaded it, then the problem might simply be that it hasn't loaded up yet. Try refreshing this page and see if this message disappears.

If you keep getting this message, you should check your site's server and error logs for any messages.

Error code: 502-backend

i got this error the code works fine on my pc when running this part of my code

@app.route('/redirect', methods=['GET'])
def redirecturl():
    params = {
        'client_id': 2773,
        'redirect_uri': 'https://www.az-sheets.com/callback',
        'response_type': 'code'  # Your Application's Callback URI
    }
    # requests.post('https://oauth.zid.sa/oauth/token', data=params)
    url = (f"https://oauth.zid.sa/oauth/authorize?{urlencode(params)}")

    return redirect(url)


@app.route('/callback', methods=['GET'])
def callback():
    payload = {
        'grant_type': 'authorization_code',
        'client_id': 2773,
        'client_secret': 'OnW69gzChkb89GNgZ8XMTKmFvtZUYK0Do5EXgBAB',

        'redirect_uri': 'https://www.az-sheets.com/callback',
        'code': request.args.get('code')  # grant code
    }
    response = requests.post('https://oauth.zid.sa/oauth/token', data=payload)

    return redirect(url_for('em', access_token=response.json()['access_token'], Authorization=response.json()['authorization'], refresh_token=response.json()['refresh_token']))





@app.route('/HAMAD', methods=['GET'])
def em():
    print(1)
    access_token = request.args.get('access_token')
    Authorization = request.args.get('Authorization')
    refresh_token = request.args.get('refresh_token')
    url = 'https://api.zid.sa/v1/managers/account/profile'
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        'Content-Type': 'application/json'
    }
    response = requests.get(url, headers=headers)


    url = 'https://api.zid.sa/v1/managers/embedded-apps-token'
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        'Content-Type': 'application/json'
    }


    data = {
        'token': str(response.json()['user']['store']['id']).translate(ecode)
    }

    rs = requests.post(url, headers=headers, json=data)

    db_handler.newstore(response.json()['user']['store']['id'],access_token,Authorization,refresh_token)

    url = "https://api.zid.sa/v1/managers/webhooks"

    payload = {
        "event": "order.status.update",
        "target_url": "https://www.az-sheets.com/zidwebhooks",
        "original_id": 1,
        "subscriber": "AZsheets",

    }
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)



    return redirect('https://web.zid.sa/market/embedded/2924')

What errors do you see in your logs? (link to logs are available on the "Web" page on PythonAnywhere)

there is no errors

What domain is that?

www.az-sheets.com

I'm getting the same error -- I've been getting emails about inability to renew my LetsEncrypt cert for days and finally checked it out today to find my site is completely inaccessible. Server logs are filled with uWSGI segfault reports apparently due to:

2024-03-20 03:59:54 Fatal Python error:  
2024-03-20 03:59:54 initfsencoding  
2024-03-20 03:59:54 :  
2024-03-20 03:59:54 Unable to get the locale encoding  
2024-03-20 03:59:54   
2024-03-20 03:59:54 ModuleNotFoundError  
2024-03-20 03:59:54 :   
2024-03-20 03:59:54 No module named 'encodings'

You need to rebuild the virtualenvs that you are using after a system image change: https://help.pythonanywhere.com/pages/ChangingSystemImage/ We've been sending out warning emails for the last year warning of the change.

Thanks very much for the help, glenn! I must not have read that page carefully enough when I changed it over and missed that part (despite the express warning at the top to pay attention to the virtualenvs section near the top).

what about my problem

We have a help page here to help you to debug issues that cause 502s: https://help.pythonanywhere.com/pages/502BadGateway/

i saw the page didn't help

Is there a specific view that causes the problem? I get a "404 not found" error on the front page of your site.

is the /redirect page you can try it using this credentials

email : bgmqqnotwn@zam-partner.email
password : 12345678

Check your error log

the error show after refreshing 502 error page i don't want 502 page to being with

Today, my website all most return 502, previous day, it work normally.

I flow link : https://help.pythonanywhere.com/pages/502BadGateway/ and try reload web app, but problem seem not resolve

I check my log, seem your system server not stable all time, sometime this work fine, but sometime it so lagging and slow

Hi there, could you share your logs please? You can send them to support@pythonanywhere.com

i have changed my code from

@app.route('/redirect', methods=['GET'])
def redirecturl():
    params = {
        'client_id': 2773,
        'redirect_uri': 'https://www.az-sheets.com/callback',
        'response_type': 'code'  # Your Application's Callback URI
    }
    # requests.post('https://oauth.zid.sa/oauth/token', data=params)
    url = (f"https://oauth.zid.sa/oauth/authorize?{urlencode(params)}")

    return redirect(url)


@app.route('/callback', methods=['GET'])
def callback():
    payload = {
        'grant_type': 'authorization_code',
        'client_id': 2773,
        'client_secret': 'OnW69gzChkb89GNgZ8XMTKmFvtZUYK0Do5EXgBAB',

        'redirect_uri': 'https://www.az-sheets.com/callback',
        'code': request.args.get('code')  # grant code
    }
    response = requests.post('https://oauth.zid.sa/oauth/token', data=payload)

    return redirect(url_for('em', access_token=response.json()['access_token'], Authorization=response.json()['authorization'], refresh_token=response.json()['refresh_token']))





@app.route('/HAMAD', methods=['GET'])
def em():
    print(1)
    access_token = request.args.get('access_token')
    Authorization = request.args.get('Authorization')
    refresh_token = request.args.get('refresh_token')
    url = 'https://api.zid.sa/v1/managers/account/profile'
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        'Content-Type': 'application/json'
    }
    response = requests.get(url, headers=headers)


    url = 'https://api.zid.sa/v1/managers/embedded-apps-token'
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        'Content-Type': 'application/json'
    }


    data = {
        'token': str(response.json()['user']['store']['id']).translate(ecode)
    }

    rs = requests.post(url, headers=headers, json=data)

    db_handler.newstore(response.json()['user']['store']['id'],access_token,Authorization,refresh_token)

    url = "https://api.zid.sa/v1/managers/webhooks"

    payload = {
        "event": "order.status.update",
        "target_url": "https://www.az-sheets.com/zidwebhooks",
        "original_id": 1,
        "subscriber": "AZsheets",

    }
    headers = {
        'Authorization': f'Bearer {Authorization}',
        'x-manager-token': f'{access_token}',
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)



    return redirect('https://web.zid.sa/market/embedded/2924')

to

@app.route('/redirect', methods=['GET'])
def redirecturl():
    params = {
        'client_id': 2773,
        'redirect_uri': 'https://www.az-sheets.com/callback',
        'response_type': 'code'  # Your Application's Callback URI
    }
    # requests.post('https://oauth.zid.sa/oauth/token', data=params)
    url = (f"https://oauth.zid.sa/oauth/authorize?{urlencode(params)}")

    return redirect(url)


@app.route('/callback', methods=['GET'])
def callback():
    payload = {
        'grant_type': 'authorization_code',
        'client_id': 2773,
        'client_secret': 'OnW69gzChkb89GNgZ8XMTKmFvtZUYK0Do5EXgBAB',
        # Your Application's Callback URI
        'redirect_uri': 'https://www.az-sheets.com/callback',
        'code': request.args.get('code')  # grant code
    }
    response = requests.post('https://oauth.zid.sa/oauth/token', data=payload)

    # def process_response():
    #     em(access_token, Authorization, refresh_token)


    global call_access_token
    call_access_token = response.json()['access_token']
    global call_Authorization
    call_Authorization = response.json()['authorization']
    global call_refresh_token
    call_refresh_token = response.json()['refresh_token']
    # threading.Thread(target=process_response).start()

    return redirect(url_for('em'))





@app.route('/HAMAD', methods=['GET'])
def em():
    print(1)
    # access_token = request.args.get('access_token')
    # Authorization = request.args.get('Authorization')
    # refresh_token = request.args.get('refresh_token')
    url = 'https://api.zid.sa/v1/managers/account/profile'
    headers = {
        'Authorization': f'Bearer {call_Authorization}',
        'x-manager-token': f'{call_access_token}',
        'Content-Type': 'application/json'
    }
    response = requests.get(url, headers=headers)


    url = 'https://api.zid.sa/v1/managers/embedded-apps-token'
    headers = {
        'Authorization': f'Bearer {call_Authorization}',
        'x-manager-token': f'{call_access_token}',
        'Content-Type': 'application/json'
    }


    data = {
        'token': str(response.json()['user']['store']['id']).translate(ecode)
    }

    rs = requests.post(url, headers=headers, json=data)
    print(rs.json())
    db_handler.newstore(response.json()['user']['store']['id'],call_access_token,call_Authorization,call_refresh_token)

    url = "https://api.zid.sa/v1/managers/webhooks"

    payload = {
        "event": "order.status.update",
        "target_url": "https://www.az-sheets.com/zidwebhooks",
        "original_id": 1,
        "subscriber": "AZsheets",

    }
    headers = {
        'Authorization': f'Bearer {call_Authorization}',
        'x-manager-token': f'{call_access_token}',
        "Content-Type": "application/json",
        "Accept": "application/json"
    }

    response = requests.post(url, json=payload, headers=headers)

    print(response.json())

    return redirect('https://web.zid.sa/market/embedded/2924')

now is working but using global isn't the best idea why my old way didn't work in your host

Glad it's working. Is there any indication about what went wrong in the web app's logs?

nothing on the logs i don't know why i can't this approach

return redirect(url_for('em', access_token=response.json()['access_token'], Authorization=response.json()['authorization'], refresh_token=response.json()['refresh_token']))

to redirect with args in your host

Definitely not the best idea. Try adding some more logging or test locally to see whats going wrong

locally works fine

and there is anther problem

"""
        <script>
        window.opener.postMessage('authorization_complete', '*');
        window.close();
        retryFetch();
        </script>
        """

retryfetch is not working was working fine on my pc

that's running in your browser not on PythonAnywhere so debug it in your browser's dev tools.

work fine from my pc

Still, we need more details to be able to help you -- usually you'd use logs / dev tools to get more debugging info and narrow down the issue to its cause.

i have fixed the browser problem still didn't figure out the redirect problem

and i have new problem now my website super slow and i have func to save user options to sql and fetch it every time he load the page if he update his options and reload the page sometimes show the old options

i made my code print the outcome

2024-03-27 04:54:20 ['id', 'order_status_name']
2024-03-27 04:54:25 ['id', 'order_status_name']
2024-03-27 04:54:29 ['id', 'order_status_name']
2024-03-27 04:54:33 Data inserted or updated successfully!
2024-03-27 04:54:39 ['order_status_name', 'id']
2024-03-27 04:54:44 ['order_status_name', 'id']
2024-03-27 04:54:48 ['id', 'order_status_name']
2024-03-27 04:55:21 ['order_status_name', 'id']

after the update one of 4 outcome is the old options

What database do you use?

Also, it looks like https://help.pythonanywhere.com/pages/GlobalStateAndWebApps/

@app.route('/get/sheetcontent', methods=['get']) def getsheetcontent(): a = db_handler.extract_data("sheetcontent") print(a) return jsonify(a)

i use pythonanywhere sql

Check the page that fjl linked to for help with the data disappearing and check this help page to help diagnose why the site is slow.

i have checked fjl link didn't apply to me

and i think slow because im in the Middle East

We do not do anything special for Middle East.

is there euw server

and what about getting old outcome of database

Yes, we have eu.pythonanywhere.com. What exactly do you mean by the "old outcome of database"?

can i transform to eu

i have func get user data from sql database (pythonanywhere sql) sometimes when i reload the page i got old data i have explained it

Sure! Just to make sure that we handle the migration without errors, could you send a message to support@pythonanywhere.com from the account's registered email address, and a subject line saying "Migrate account monsteruk from US to EU", linking to this forum thread in the body? Then we can get the process kicked off.

is there anything different in euw server payment or something ?

.

The prices are in EUR, so the cost is a little bit higher.