Forums

Flask app running locally using ngrok to tunnel - now want to move it on to pythonanywhere

Hi

Struggling to get this to run on Python Anywhere. What currently happens is that I run this script in powershell - I then use Ngrok to tunnel to my local host. The ngrok url is provided as a webhook,which then redirects to my local host to recieve the data and then update an API.

I would like to move this off my local machine. Any tips much appreciated...

p.s i've attached the error log when I first launch the web app on this platform (See below code)

Thanks

from flask import Flask, request, Response
import json
import urllib as ul
from mechanize import Browser



app = Flask(__name__)

@app.route('/' , methods=['GET','POST'])  
def abtrigger():
    if request.method == 'GET':
            challenge = request.args.get('challenge')
            return challenge
            resp = Response(challenge,status='200')  # first respond back to Adobe with 200 status and challenge
            return resp 
    else:
            print (request.is_json)

            content = request.get_json()

            mcid = content ['event']['com.adobe.mcloud.pipeline.pipelineMessage']['com.adobe.mcloud.protocol.trigger']['mcId']

            try:
                tracking_id = content ['event']['com.adobe.mcloud.pipeline.pipelineMessage']['com.adobe.mcloud.protocol.trigger']['enrichments']['analyticsHitSummary']['dimensions']['eVar56']['data'][0]

                print tracking_id
                print mcid
                url = 'http://xxxxx.tt.xxxx.net/m2/xxxxxxxx/profile/update?mbox3rdPartyId='+tracking_id+'&profile.attr=0&'+adobe_profile+''
                print url
                br =  Browser()
                br.set_handle_robots(False)
                response = br.open(url)
                response.read()
                print response
                return 'JSON posted'

            except IndexError:

                print "no tracking_id"
                return 'No tracking ID'



if __name__ == '__main__':

    context = ('local.crt', 'local.key')
    app.run()

error log:

2018-05-24 17:21:26,727: [2018-05-24 17:21:26,726] ERROR in app: Exception on / [GET]
2018-05-24 17:21:26,728: Traceback (most recent call last):
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-24 17:21:26,728:     response = self.full_dispatch_request()
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
2018-05-24 17:21:26,728:     response = self.make_response(rv)
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response
2018-05-24 17:21:26,728:     raise ValueError('View function did not return a response')
2018-05-24 17:21:26,729: ValueError: View function did not return a response
2018-05-24 17:21:26,726: Exception on / [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request#012    response = self.make_response(rv)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response#012    raise ValueError('View function did not return a response')#012ValueError: View function did not return a response
2018-05-24 17:21:50,486: [2018-05-24 17:21:50,486] ERROR in app: Exception on / [GET]
2018-05-24 17:21:50,486: Traceback (most recent call last):
2018-05-24 17:21:50,486:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-24 17:21:50,486:     response = self.full_dispatch_request()
2018-05-24 17:21:50,487:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
2018-05-24 17:21:50,487:     response = self.make_response(rv)
2018-05-24 17:21:50,487:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response
2018-05-24 17:21:50,487:     raise ValueError('View function did not return a response')
2018-05-24 17:21:50,487: ValueError: View function did not return a response
2018-05-24 17:21:50,486: Exception on / [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request#012    response = self.make_response(rv)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response#012    raise ValueError('View function did not return a response')#012ValueError: View function did not return a response

The web app reload code tries to hit your web app root url to check that your web app is working. It does not provide any arguments. In your view, if your get a get request, you immediately return the "challenge" argument, which will be None, so your view is not returning a response and that is what causes the error.

Thanks for the reply Glenn. What is the web app reload code? How would I get round this? The script works fine on my local host. Cheers

The web app reload code is the code that we wrote to reload your web app. What I was trying to say is that when your site root url is accessed without parameters, it doesn;'t return a response, because it returns the challenge parameter, which is None, because it's not there.

Thanks Glenn - I tested again, with a query parameter in the URL and I got back the correct response. I then tried testing with a POST to the URL with json in the body (to test i used postman).

I get the following error log - any ideas what's going wrong? Apologies for all the questions, first time I've moved a script from my own device to the "cloud".

Cheers,

 2018-05-24 17:21:26,727: [2018-05-24 17:21:26,726] ERROR in app: Exception on / [GET]
2018-05-24 17:21:26,728: Traceback (most recent call last):
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-24 17:21:26,728:     response = self.full_dispatch_request()
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
2018-05-24 17:21:26,728:     response = self.make_response(rv)
2018-05-24 17:21:26,728:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response
2018-05-24 17:21:26,728:     raise ValueError('View function did not return a response')
2018-05-24 17:21:26,729: ValueError: View function did not return a response
2018-05-24 17:21:26,726: Exception on / [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request#012    response = self.make_response(rv)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response#012    raise ValueError('View function did not return a response')#012ValueError: View function did not return a response
2018-05-24 17:21:50,486: [2018-05-24 17:21:50,486] ERROR in app: Exception on / [GET]
2018-05-24 17:21:50,486: Traceback (most recent call last):
2018-05-24 17:21:50,486:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-24 17:21:50,486:     response = self.full_dispatch_request()
2018-05-24 17:21:50,487:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
2018-05-24 17:21:50,487:     response = self.make_response(rv)
2018-05-24 17:21:50,487:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response
2018-05-24 17:21:50,487:     raise ValueError('View function did not return a response')
2018-05-24 17:21:50,487: ValueError: View function did not return a response
2018-05-24 17:21:50,486: Exception on / [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request#012    response = self.make_response(rv)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response#012    raise ValueError('View function did not return a response')#012ValueError: View function did not return a response
2018-05-24 17:34:32,823: [2018-05-24 17:34:32,823] ERROR in app: Exception on / [GET]
2018-05-24 17:34:32,823: Traceback (most recent call last):
2018-05-24 17:34:32,823:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-24 17:34:32,823:     response = self.full_dispatch_request()
2018-05-24 17:34:32,823:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
2018-05-24 17:34:32,824:     response = self.make_response(rv)
2018-05-24 17:34:32,824:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response
2018-05-24 17:34:32,824:     raise ValueError('View function did not return a response')
2018-05-24 17:34:32,824: ValueError: View function did not return a response
2018-05-24 17:34:32,823: Exception on / [GET]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request#012    response = self.make_response(rv)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1731, in make_response#012    raise ValueError('View function did not return a response')#012ValueError: View function did not return a response
2018-05-29 08:33:38,089: [2018-05-29 08:33:38,073] ERROR in app: Exception on / [POST]
2018-05-29 08:33:38,091: Traceback (most recent call last):
2018-05-29 08:33:38,091:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-29 08:33:38,091:     response = self.full_dispatch_request()
2018-05-29 08:33:38,091:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2018-05-29 08:33:38,091:     rv = self.handle_user_exception(e)
2018-05-29 08:33:38,091:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2018-05-29 08:33:38,092:     reraise(exc_type, exc_value, tb)
2018-05-29 08:33:38,092:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2018-05-29 08:33:38,092:     rv = self.dispatch_request()
2018-05-29 08:33:38,092:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2018-05-29 08:33:38,092:     return self.view_functions[rule.endpoint](**req.view_args)
2018-05-29 08:33:38,092:   File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger
2018-05-29 08:33:38,092:     response = br.open(url)
2018-05-29 08:33:38,093:   File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open
2018-05-29 08:33:38,093:     return self._mech_open(url, data, timeout=timeout)
2018-05-29 08:33:38,093:   File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open
2018-05-29 08:33:38,093:     raise response
2018-05-29 08:33:38,093: httperror_seek_wrapper: HTTP Error 403: Forbidden
2018-05-29 08:33:38,073: Exception on / [POST]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request#012    rv = self.handle_user_exception(e)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception#012    reraise(exc_type, exc_value, tb)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request#012    rv = self.dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request#012    return self.view_functions[rule.endpoint](**req.view_args)#012  File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger#012    response = br.open(url)#012  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open#012    return self._mech_open(url, data, timeout=timeout)#012  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open#012    raise response#012httperror_seek_wrapper: HTTP Error 403: Forbidden
2018-05-29 08:38:18,463: [2018-05-29 08:38:18,461] ERROR in app: Exception on / [POST]
2018-05-29 08:38:18,463: Traceback (most recent call last):
2018-05-29 08:38:18,463:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2018-05-29 08:38:18,463:     response = self.full_dispatch_request()
2018-05-29 08:38:18,464:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2018-05-29 08:38:18,464:     rv = self.handle_user_exception(e)
2018-05-29 08:38:18,464:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2018-05-29 08:38:18,464:     reraise(exc_type, exc_value, tb)
2018-05-29 08:38:18,464:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2018-05-29 08:38:18,464:     rv = self.dispatch_request()
2018-05-29 08:38:18,464:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2018-05-29 08:38:18,464:     return self.view_functions[rule.endpoint](**req.view_args)
2018-05-29 08:38:18,464:   File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger
2018-05-29 08:38:18,465:     response = br.open(url)
2018-05-29 08:38:18,465:   File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open
2018-05-29 08:38:18,465:     return self._mech_open(url, data, timeout=timeout)
2018-05-29 08:38:18,465:   File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open
2018-05-29 08:38:18,465:     raise response
2018-05-29 08:38:18,465: httperror_seek_wrapper: HTTP Error 403: Forbidden
2018-05-29 08:38:18,461: Exception on / [POST]#012Traceback (most recent call last):#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012    response = self.full_dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request#012    rv = self.handle_user_exception(e)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception#012    reraise(exc_type, exc_value, tb)#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request#012    rv = self.dispatch_request()#012  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request#012    return self.view_functions[rule.endpoint](**req.view_args)#012  File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger#012    response = br.open(url)#012  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open#012    return self._mech_open(url, data, timeout=timeout)#012  File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open#012    raise response#012httperror_seek_wrapper: HTTP Error 403: Forbidd

See http://help.pythonanywhere.com/pages/403ForbiddenError/

Thanks Glenn - I've upgraded to a paid sub. I will try again.

Interesting - still getting the same error. Do I still need to whitelist ?

2018-05-29 13:23:44,810: [2018-05-29 13:23:44,808] ERROR in app: Exception on / [POST] 2018-05-29 13:23:44,810: Traceback (most recent call last): 2018-05-29 13:23:44,810: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app 2018-05-29 13:23:44,810: response = self.full_dispatch_request() 2018-05-29 13:23:44,810: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request 2018-05-29 13:23:44,810: rv = self.handle_user_exception(e) 2018-05-29 13:23:44,810: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception 2018-05-29 13:23:44,811: reraise(exc_type, exc_value, tb) 2018-05-29 13:23:44,811: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request 2018-05-29 13:23:44,811: rv = self.dispatch_request() 2018-05-29 13:23:44,811: File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request 2018-05-29 13:23:44,811: return self.view_functionsrule.endpoint 2018-05-29 13:23:44,811: File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger 2018-05-29 13:23:44,811: response = br.open(url) 2018-05-29 13:23:44,811: File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open 2018-05-29 13:23:44,811: return self._mech_open(url, data, timeout=timeout) 2018-05-29 13:23:44,811: File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open 2018-05-29 13:23:44,811: raise response 2018-05-29 13:23:44,811: httperror_seek_wrapper: HTTP Error 403: Forbidden 2018-05-29 13:23:44,808: Exception on / [POST]#012Traceback (most recent call last):#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app#012 response = self.full_dispatch_request()#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request#012 rv = self.handle_user_exception(e)#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception#012 reraise(exc_type, exc_value, tb)#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request#012 rv = self.dispatch_request()#012 File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request#012 return self.view_functionsrule.endpoint#012 File "/home/ned06/mysite/flask_app.py", line 43, in abtrigger#012
response = br.open(url)#012 File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 203, in open#012 return self._mech_open(url, data, timeout=timeout)#012 File "/usr/local/lib/python2.7/dist-packages/mechanize/_mechanize.py", line 255, in _mech_open#012 raise response#012httperror_seek_wrapper: HTTP Error 403: Forbidden

If you're still getting that after you've upgraded and reloaded your web app, then the 403 is coming from the site you're hitting and you need to authenticate with it.

Thanks - the URL i am trying to access doesn't require any authentication. Any other ideas?

Script works on my laptop and I have checked the python anywhere server logs to confirm the URL has been created correctly.

Thanks once again

fixed :) - i needed to reload the app again

Sorry Glenn, one more question. From a GDPR, data governance perspective... Where are the servers located? Do you store any data - i.e server logs? Or do they expire? Thanks

Our servers are located in the US. See our Terms and Privacy and Cookies policy for other details.