python Script Framework webpy Getting Started Installation and Application Creation
- 2021-12-13 08:49:04
- OfStack
Directory 1: Installation 2: URL Processing 3: Class 4: Creating 1 Application 5: Instance
1: Installation
pip install web.py
Or
http://xiazai.ofstack.com/202111/yuanma/web.py-0.38_jb51.rar
2: URL treatment
The most important part of any website is its URL structure.
urls = ('/', 'Index',) # defines a mapping rule that sends requests for the '/' virtual path to the Index class for processing.
3: Class
A class of Index is defined to handle the request of '/', which can be handled according to its own requirements, which may be GET, POST...
class Index:
def GET(self) :
return ' Hello, everyone '
4: Create an application
app=web.application(urls,globals()) # Create app Object
app.run() # Start app
5: Examples
import web
#web The most basic components
#1.urls Routing table
#2.1 A web.application Instances app
#3. Start app
urls Yes url Mapping rules, similar to ( servert )
urls=('/','Index')
# This sentence means that it will be sent to ' / ' The request for this virtual path is handed over to Index Class to handle
# This url Variable design for the whole website 1 A URL Control scheme
# Definition 1 A Index Class, handling of routes
class Index:
def GET(self):
# Prevent Chinese garbled codes
web.header('Content-Type','text/html;charset=UTF-8')
# Your operation You can return str, Documents ,html
# return "get Request! "
# return open(r'F:\GitHub\Python\MyWeb\tesseract.log')
return '<h1>GET Request </h1>'
def POST(self):
return 'post Request! '
# Create 1 Applications
app=web.application(urls,globals())
#urls Parameter specifies the Web site url And the function executed by the application 1 Mapping, but you can see urls Yes 1 A tuple with only strings in it
#globals() Will return 1 A dictionary-like object that contains all the variables, functions, classes, and modules in the current space. The key is the name of these things, and the value is the response object, so that the object can be obtained by name.
if __name__ == '__main__':
app.run()
The above is the python script framework webpy entry installation and application creation details, more about webpy entry installation and application creation information please pay attention to other related articles on this site!