Python Web py Getting Started -3-url mapping

Source: Internet
Author: User

This article describes the URL mapping, before we introduce the mapping, we first understand what is the URL? The URL (uniform/universal Resource locator abbreviation, Uniform Resource Locator) is a concise representation of the location and access to resources available on the Internet and is the address of standard resources on the Internet. Each file on the Internet has a unique URL that contains information about the location of the file and how the browser should handle it. Know what is a URL, then URL mapping is good to understand. White words on the basis of the matching rules of the URL to find the corresponding page.


1.URL three kinds of mappings

1 URL exact match, format:/index

Explain, in the browser input http://localhost:8080/index, then will exactly match "/index" such a page.

2 URL fuzzy matching, format:/index\d+

Explanation: This means starting with "/index," followed by a number or a number of URLs such as a URL to match, for example, browser input: http://localhost:8080/index123 or http://localhost:8080/ Index4, this kind of fuzzy matching, generally requires the use of regular expressions.

3 URL with group matching, format/index (\d+)

Explanation: This band match seems to match the above Blur, because adding a bracket outside the regular expression means matching a group.


2. Modify our previous hello.py content to practice URL mapping

Import Web
        
urls = (
	'/index ', ' Index ',
	'/blog/\d+ ', ' blog ',
    '/(. *) ', ' Hello '
)
app = Web.application (URLs, Globals ())

class Hello:        
    def get (self, name): Return
        ' hello ' + name

class index:< C10/>def Get (self): return
		"Index method"

class Blog:
	def get (self): return
		' blog Get method '
	
	def POST (self): return
		' blog POST method '

if __name__ = = "__main__":
    App.run ()
Through the code above, we can see that we write URL mapping to the structure of URLs, the top match is a complete match, the matching result range is very small, the middle is fuzzy match, the matching range is larger, and the third is any match (. *) to represent any character, the result range is larger. This need to maintain such norms, from small to large range.

3. Test run a different URL to verify that the set match is successful

3.1 Full match


3.2 Fuzzy Matching


3.3 Any match


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.