An analysis of how to set URLs in the Python web. py framework

Source: Internet
Author: User
Web. py is a lightweight Web development framework of Python. here we will analyze Python web. the url setting method in the py framework. if you need a friend, you can refer to the data on the webpage in the following two methods: GET and POST. GET transmits parameters in the form of a url on the web. py has a good match, if we configure the following urls

 urls =(  '/','index',  '/weixin/(.*?)','WeixinInterface'    )

Do not consider/weixin/. now we will write the index class.

 class index:  def GET(self):    i = web.input(name = 'kevinkelin',age = 100)         return render.index(i.name,i.age)

Write an index.html template file

 $def with(name,age)$if name:  I just want to say hello to $name, he is $age years old$else:  hello,world!

When http: // 127.0.0.1: 8080/is accessed, the value of name and age is not passed. because the default name and age values are defined in my GET function, therefore, the program will pass kevinkelin and 26 to the template to get the following output:

I just want to say hello to kevinkelin, he is 100 years old

When accessing http: // 127.0.0.1: 8080 /? Name = yyx & age = 26: the following output is obtained when name = yyx and age = 26 is passed to the GET function:

I just want to say hello to yyx, he is 26 years old

You can also leave the default parameter empty.

i = web.input(name = None,age = None)

When you access http: // 127.0.0.1: 8080/, you will get hello, world! The output is the else in the template.
However, if you do not define the name or age, an error will occur.

i = web.input()

This is because you will allocate I. name and I. age to the template later, but the two variables are not in the global variables, so an error will be reported.
But sometimes we want to pass the parameter like this and don't want to add the "?" Now we have to change the urls rule.

 urls =(  '/name=(.*)&age=(.*)','index',  '/weixin/(.*?)','WeixinInterface'    )

Re-write the class index

 class index:  def GET(self,name,age):    return render.index(name,age)

Here, the url parameters are matched through regular expressions and then passed to the GET parameter in the index class.
When you access http: // 127.0.0.1: 8080/name = yyx & age = 26

I just want to say hello to yyx, he is 26 years old

The second method seems simple, but it is not easy to control. it requires more work to write regular expressions.
If I want to know how many parameters are passed through the GET method, I can directly return to see which parameters are passed.
Next, let's take a look at the post data:
We can create a simple form or directly use fiddler to construct data for POST value passing.

def POST(self):    data = web.data()        return data

I want to see the data type.

return type(data)

The result is: That is to say, web. py has converted the post data to the str type.
Let me try to pass xml

 
 
  
   yanxingyang
  
  
   study_python
  
  
   123456
  
  
   text
  
  
   Just a test
  
 

In fact, the XML format has been changed. I will try to use lxml to parse it.

from lxml import etreedata = web.data()xml = etree.fromstring(data)content = xml.find(‘Content').textreturn content

The result is good.

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.