
!pip install Flask
Requirement already satisfied: Flask in /home/woong/ppp/.vppp/lib/python3.9/site-packages (3.1.0) Requirement already satisfied: Werkzeug>=3.1 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (3.1.3) Requirement already satisfied: Jinja2>=3.1.2 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (3.1.4) Requirement already satisfied: itsdangerous>=2.2 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (2.2.0) Requirement already satisfied: click>=8.1.3 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (8.1.8) Requirement already satisfied: blinker>=1.9 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (1.9.0) Requirement already satisfied: importlib-metadata>=3.6 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Flask) (8.5.0) Requirement already satisfied: zipp>=3.20 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from importlib-metadata>=3.6->Flask) (3.21.0) Requirement already satisfied: MarkupSafe>=2.0 in /home/woong/ppp/.vppp/lib/python3.9/site-packages (from Jinja2>=3.1.2->Flask) (3.0.2)
from flask import Flask
app = Flask(__name__)
print(__name__)
__main__
1. What is '__name__'?¶
__name__ is a built-in variable in Python that indicates the name of the module.
- When the file is executed directly, the value of __name__ is set to "__main__".
- When the file is imported as a module, the value of name is set to the name of the module (usually the filename without the .py extension).
1.1 When is it used?¶
- Entry Point for Scripts: Used in if name == "main": to ensure certain code runs only when the script is executed directly, not when imported as a module.
- Testing and Debugging: Allows writing test or debugging code in a module that executes only when the module is run directly.
1.2 Entry Point¶
Languages like C#, Java, C, and C++ have a defined entry point, where the program starts execution from a specific main function.
1.3 Python's Entry Point¶
Python, as a scripting language, does not have a traditional entry point.
It starts execution from the first line of the script being run.
However, the if name == "main": statement allows implementing functionality similar to an entry point.
2. Routing¶
2.1. What is a URL?¶
Uniform Resource Locator
A convention for specifying the location of a resource on the internet.
One of the main components of the World Wide Web: HTML, URL, HTTP.
Reference: URL vs URI¶
URI (Uniform Resource Identifier): A general identifier for a resource.
A URL is a specific type of URI.
- For example, "https://www.google.com" is both a URL and a URI representing the google server.
- However, in "https://www.google.com/input?id=kim&pw=1111", "https://www.google.com/input" is a URL,
while "https://www.google.com/input?id=kim&pw=1111" is a URI because it includes the identifier "?id=kim&pw=1111" required to retrieve specific information.
2.2. What is Routing?¶
A function that directs to the appropriate destination.
Routing connects a URL to the function that handles that URL.
- Example: "http://www.woongkeol.com/Blogs?urlHandle=CSharp"
- On the server "http://www.woongkeol.com", it calls the function associated with the Blogs destination (where "?urlHandle=CSharp" is passed as a parameter to the function).
# Route for the URL '/hello'
@app.route("/hello")
def hello():
# Returns 'Hello World!' as an HTML heading
return "<h1>Hello World!</h1>"
In the code above, @ is called a decorator (which will be explained separately).
3. Running the Flask Web Server as the Main Module¶
The server can be started using the app.run() function.
You can configure the IP and port for the server by passing them as options.
- The most commonly used parameters are host, port, and debug.
- host: Specifies the web address (e.g., '127.0.0.1' or '0.0.0.0').
- port: Specifies the port number (e.g., 5000).
- debug: A boolean parameter (True or False) that enables or disables debug mode.
When enabled, it automatically reloads the server when code changes and provides detailed error messages in the browser.
Example: app.run(host=None, port=None, debug=True)
Additional Information:¶
- When host is set to None, Flask will use the default 127.0.0.1 (localhost).
- If host is set to '0.0.0.0', the app will be accessible from any machine in the network (not just locally).
- When debug=True, Flask will run in debug mode, which is helpful for development as it shows detailed error logs and enables live reloading when changes are made to the code.
3.1 Web Server and WAS Framework¶
A web server primarily handles static HTML pages.
- It processes client requests and returns static data, such as HTML, CSS, or image files.
To handle dynamic content or interactive features, a WAS (Web Application Server) framework is required.
- Major WAS frameworks include Flask, Django, Ruby on Rails, Node.js, and others.
※ For production environments, it is recommended to use a dedicated and professional web server (e.g., Nginx, Apache)
in conjunction with a WAS framework. However, Flask provides a basic built-in web server for development and testing purposes with simple commands.
4. Development Process¶
Implementing a web service on your own PC:
- Set the Host to
localhost
,127.0.0.1
, or0.0.0.0
. - Use the
app.run()
function to run a built-in web server.
# Host address to bind the web server
host_addr = "0.0.0.0"
# Port number to run the web server
port_num = "8080"
# Entry point of the application
if __name__ == "__main__":
# Start the web server
app.run(host=host_addr, port=port_num)
* Serving Flask app '__main__' * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:8080 * Running on http://172.22.109.123:8080 Press CTRL+C to quit
# Full Base Code
from flask import Flask
app = Flask(__name__)
# Route for the URL '/hello'
@app.route("/hello")
def hello():
# Returns 'Hello World!' as an HTML heading
return "<h1>Hello World!</h1>"
# Host address to bind the web server
host_addr = "127.0.0.1"
# Port number to run the web server
port_num = "8080"
# Entry point of the application
if __name__ == "__main__":
# Start the web server
app.run(host=host_addr, port=port_num)
* Serving Flask app '__main__' * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:8080 Press CTRL+C to quit 127.0.0.1 - - [29/Dec/2024 20:53:48] "GET / HTTP/1.1" 404 - 127.0.0.1 - - [29/Dec/2024 20:54:43] "GET /hello HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2024 20:55:17] "GET /hello HTTP/1.1" 200 - 127.0.0.1 - - [29/Dec/2024 20:55:19] "GET /hello HTTP/1.1" 200 -
127.0.0.1 - - [29/Dec/2024 20:55:26] "GET /hello HTTP/1.1" 200 -