Forums

Does pythonanywhere.com allow server to server communication?

I am facing challenges in achieving this. I have searched on Google and in this forum and in stackoverflow, however i didn't find any result.

can you give more details on what you mean by server to server communication? (eg: from which server to which server? what type of communication protocol? how are you doing this communication?)

Hi, it's API communication between Pythonanywhere server and my other server. When I tried to send a communication request from the other server to pythonanywhere, I got this return message at tinker:1: Failed to load "https://insurevite.pythonanywhere.com/upload: No "Access-control-All;ow-Origin" header is present on the requested resource. Origin "http://travel.localhost" is therefore not allowed access

That is your site that is rejecting the request because you have not set the access-control-allow-origin header in the response for the upload path of your web app.

My server is PHP. Could that be the reason this python anywhere server is unable to communicate?

No. That would not be the reason. The reason is you did not set the access-control-allow-origin header in your pythonanywhere webapp.

You mean our php webapp or pythonanywhere webapp?

The PythonAnywhere one. It needs to set that header when requests come in on that URL -- the header says that POST requests are allowed from the PHP site. If the URL in question is one that people should be able to access via code, you might want to consider setting the value of the header to "*".

Mozilla have some really good docs about that header and what it means.

Do you have a step by step process about how to do it in pythonanywhere.com platform?

I am trying to fix this by following this and this solution

and i installed flask_cors by opening bash at below locations

/home/InsureVite/.local/lib/python3.6
/home/InsureVite/.local/lib/python2.7

and typed pip install -U flask-cors --user in both. It successfully installed after which, i went to web tab in pythonanywhere.com and clicked on refresh.

When i go to the app url, its broken. I checked the error log and below are the messages. Please suggest

2018-11-07 11:06:57,931: Error running WSGI application
2018-11-07 11:06:57,936: ImportError: No module named flask_cors
2018-11-07 11:06:57,936:   File "/var/www/insurevite_pythonanywhere_com_wsgi.py", line 16, in <module>
2018-11-07 11:06:57,936:     from flask_app import app as application  # noqa
2018-11-07 11:06:57,937: 
2018-11-07 11:06:57,937:   File "/home/InsureVite/.virtualenvs/myvirtualenv3/flask_app.py", line 10, in <module>
2018-11-07 11:06:57,937:     from flask_cors import CORS
2018-11-07 11:06:57,937: ***************************************************
2018-11-07 11:06:57,937: If you're seeing an import error and don't know why,
2018-11-07 11:06:57,938: we have a dedicated help page to help you debug: 
2018-11-07 11:06:57,938: https://help.pythonanywhere.com/pages/DebuggingImportError/
2018-11-07 11:06:57,938: ***************************************************

The location where you run the pip install command doesn't make any difference; to install something for Python 3.6, use the pip3.6 command. See this help page for more information.

As suggested, i executed the pip statement and below is the bash log.

14:34 ~ $ pip3.6 install -U flask-cors --user
Requirement already up-to-date: flask-cors in ./.local/lib/python3.6/site-packages
Requirement already up-to-date: Flask>=0.9 in ./.local/lib/python3.6/site-packages (from flask-cors)
Requirement already up-to-date: Six in ./.local/lib/python3.6/site-packages (from flask-cors)
Requirement already up-to-date: Jinja2>=2.10 in ./.local/lib/python3.6/site-packages (from Flask>=0.9->flask-cors)
Requirement already up-to-date: itsdangerous>=0.24 in ./.local/lib/python3.6/site-packages (from Flask>=0.9->flask-cors)
Requirement already up-to-date: click>=5.1 in ./.local/lib/python3.6/site-packages (from Flask>=0.9->flask-cors)
Requirement already up-to-date: Werkzeug>=0.14 in ./.local/lib/python3.6/site-packages (from Flask>=0.9->flask-cors)
Requirement already up-to-date: MarkupSafe>=0.23 in ./.local/lib/python3.6/site-packages (from Jinja2>=2.10->Flask>=0.9->flask-cors)
14:35 ~ $

After this, i reloaded the app in web tab. When i re-opened the api link, it gave me the same error and nothing changed. Same issue persists.

If i simply run from flask_cors import CORS in the python console, it successfully executes, but not with my API. Please suggest.

The "3.6" in my previous post was just an example -- what you need to do is find out which version of Python your website is using (it's displayed on the "Web" page, then use the corresponding pipX.Y command. If you're using Python 2.7 for the website, use pip2.7, if you're using Python 3.4, use pip3.4, and so on.

Additionally, if you're using a virtualenv, then you should install it into that virtualenv. Again, virtualenv details are displayed on the "Web" page, and there's also a link there to start a Bash console inside the virtualenv so that you can run pip install there.

You may also need to reload the website using the button on the "Web" page to make it pick up newly-installed modules.

Just check the 5th comment above where i installed in both environments. I have already done everything that you said, still its not working. Please suggest. Issue is not installing with package as i have already installed a long time ago in virtual environment. Please re-read the thread if you aren't able to understnd.

You said

i installed flask_cors by opening bash at below locations

/home/InsureVite/.local/lib/python3.6 /home/InsureVite/.local/lib/python2.7 and typed pip install -U flask-cors --user in both

That's not the same thing as installing it into a virtual environment. To install a package into a virtual environment, you need to start a bash console, activate the virtual environment, and then use pip install (without the "--user" flag) to install the package.

The quickest way to start a bash console with your virtualenv activated is to use the link on the "Web" page. On that page, about half-way down, you'll see a section with the title "Virtualenv". In that section, there will be a link saying "Start a console in this virtualenv". Click that link. This will start a console with your virtualenv activated. In that console, run the command

pip install -U flask-cors

Then reload your web app, and the import error should be fixed.