Create a main.py file and paste the following code
After right click to run Debug ' main ', the debug window below will appear importerror:no module named ' Bottle ' Prompt to import bottle package error
So we need to install these two packages first, cmd, enter pip install bottle Enter after start install Bottle Pack Mac permissions not enough to fill up sudo
#!/usr/bin/python#Coding=utf-8 fromBottleImportDefault_app, GET, run fromBeaker.middlewareImportSessionmiddleware#Set Session ParametersSession_opts = { 'Session.type':'file', 'Session.cookie_expires': 3600, 'Session.data_dir':'/tmp/sessions/simple', 'Session.auto': True} @get ('/index/')defcallback ():return 'Hello world!'#function Main entranceif __name__=='__main__': APP_ARGV=Sessionmiddleware (Default_app (), session_opts) run (app=APP_ARGV, host='0.0.0.0', port=9090, Debug=true, Reloader=true)
We open the browser and enter http://127.0.0.1:9090/index/in the address bar to see Hello World.
Detailed:
#!/usr/bin/python This is the identity that the current script specifies to execute it with Python, in order to prevent the user from installing Python in the default/usr/bin path, the system executes first in the Env setting to find the Python installation path, Then call the Python program under the corresponding path to execute. # Coding=utf-8 This line indicates that the current file is using the Utf-8 encoding 4th and 5th lines to import the bottle package and the specified module of the Beaker package because the bottle framework itself does not provide support for the session, So use Beaker middleware to implement. Line 7th to 13th, is to create a session configuration dictionary, used to store the session storage type is the file type, session expiration time is 3,600 seconds, session file storage path is/tmp/sessions/simple (Files stored in the Linux system TMP directory, the system will be automatically cleaned up regularly) Line 15th to 17th, we define a/index/route for browser access, you can use this path to locate here (that is, the previous browser input URL address, Here we can arbitrarily define as any route can be, the way to access the route we define as a get method, of course, can also be defined as post, put, delete and other ways to access the route. Line 20th, which identifies the current location as the main entrance of the program, when running the program, starting from here Line 21st, initialize the session parameter 22nd line, start the WSGI Web program, address the local address, access Port 9090, start Debug, Finally there is a parameter reloader to automatically restart the Web service and reload the modified code when the project code is modified
Python Output Hello World (detailed command of each line)