Analysis of the method for setting URLs in the Python web. py framework, pythonweb. py

Source: Internet
Author: User

Analysis of the method for setting URLs in the Python web. py framework, pythonweb. py

The data in the webpage can be transmitted in the GET and POST modes. The GET parameters are transmitted in the form of a URL, which has a good match in web. py. 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 <em>hello</em> to $name, he is $age years old$else:  <em>hello</em>,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 <type 'str'>, that is, the web. py has converted the post data to the str type.
Let me try to pass xml

 <xml><ToUserName>yanxingyang</ToUserName><FromUserName>study_python</FromUserName><CreateTime>123456</CreateTime><MsgType>text</MsgType><Content>Just a test</Content></xml>

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.