The browser displays my python code instead of running it

Asked

Viewed 861 times

-1

When accessing through the browser my Python page, it displays the code and does not execute the script.

Follows my code:

#!/usr/bin/python
# cabecalho que informa o browser para renderizar como HTML
print "Content-Type: text/html\n\n"
# o conteudo em si
print "<strong>Hello, world!</strong>"
  • 3

    The browser does not interpret Python. The only language the browser interprets is Javascript. If you want to work with CGI, you need a server (such as Apache or Nginx) and the Python module installed on it (mod_wsgi or uWsgi), so that the SERVER generates HTML pages and sends them to the browser. That way the browser will only show you the same text.

  • @inovapixel How do I work on the web with Python?

  • 1

    It’s like I said, you need a server running the module responsible for interpreting Python. This is called CGI (common gateway interface), and can be done with Python, Perl, C/C++, and some other languages... These languages are multi-purpose (multi-purpose), they’re not specific to the Web, so getting started with them is more difficult. I recommend that if you want to learn how to program for the Web, start with Php, because it comes ready to run, just download and install programs like XAMPP, WAMPSERVER, VERTRIGO SERVER and etc... They already come with everything ready to program in Php.

  • 1

    The easiest way to get started with python on the web is by using a framework, there are dozens of them, choose a popular Fullstack (Django, Turbogears and Web2py, my favorite is Django) See a list, here.

1 answer

0

Open your terminal and install the flask.

To install the flask suffice...

pip install flask

python3 -c "import flask; print(flask.version)"

To run your web application, first tell Flask where to find the application (the Helly.py file in your case) with the FLASK_APP environment variable:

export FLASK_APP=hello

Then run it in development mode with the FLASK_ENV environment variable:

export FLASK_ENV=development

Finally, run the application using the flask run command:

flask run

Browser other questions tagged

You are not signed in. Login or sign up in order to post.