2013年10月10日星期四

Crusher Django Tutorial(7) About HTTP In Django

Cute and hate ADs: A suck website I made(For god sake help me click it or just ignore it.55555555)

Well, in this post, we will learn something about the HTTP Protocol which will be awesome!

As we know, the web app is based on the HTTP Protocol.HTPP Protocol is based on the TCP/IP Protocol which is the basic of the Internet.Basically, the Internet is just the routers and switchers to help us get the data from a server.Now, we just see what is the HTTP info in the Web layer.

In Django,we can use request.meta as a dictionary for store the available HTTP headers for the request from the clients.So what we need to do is just show the info in the dictionary, just using the HttpResponse()

Add the display_meta() function in the TomAndJerry views.py:

 

def display_meta(request):
    values = request.META.items()
    values.sort()
    html = []
    for k, v in values:
        html.append("<tr><td>%s</td><td>%s</td></tr>" % (k, v))
    return HttpResponse('<table>%s</table>' % '\n'.join(html))
 ;

Then, conf the urls.py as we learn before.But for now, we just want to use the template to make our page more beautiful.

Edit a template file in the folder templates named meta.html:

 
<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstarp.css" type="text/css" media="screen" title="no title" charset="utf-8" />
    <link rel="stylesheet" href="/static/table.css" type="text/css" media="screen" title="no title" charset="utf-8" />
    <title>META INFO FROM DJANGO</title>
</head>
<body>
 <h1>HTTP HEAD INFO</h1>
 {% if values %}
 <table class="table">
     <h2><caption>The info in the head:</caption></h2>
            {% for k, v in values %}
            <tr class="info">
                <td>{{ k }}</td>
                <td class="vinfo">{{ v }}</td>
            </tr>
            {% endfor %}
 </table>
 {% else %}
     <h2 class="error">Error</h2>
 {% endif %}
</body>
</html>
 

And we add a little css code for the table to show the info.

 
td {
 border: 1px solid green;
}
 

Now we re-edit the display_meta() views function:

 
def display_meta(request):
    values = request.META.items()
    values.sort()
    return render_to_response("meta.html", locals())
 

And don't forget to conf the urls.py elsewise you will get the 404 error which means no such page.

Now, we just test our info page:

LOL...It works.And can see a lot info.

Here is something important:

  • HTTP_HOST 127.0.0.1:8000 which show the server IP address and the port server use
  • HTTP_USER_AGENT Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.22 (KHTML, like Gecko) Ubuntu Chromium/25.0.1364.160 Chrome/25.0.1364.160 Safari/537.22 The USER_AGENT means the browser I use.
  • HTTP_COOKIE sessionid=8d2469b16fc859800c481ea613bf781b Which show the session id.It changes by the browser and computer.And it is a bit complex.
  • PWD /home/zoo/hello The current execute path we use.
  • PATH_INFO /display/ The file path we are in
  • REQUEST_METHOD GET The request method we use is get not post, it is not a form, usually we use post method in a form.
  • SERVER_NAME localhost The Server Name is localhost.
  • SERVER_PORT 8000 The Server port is 8000
  • SERVER_PROTOCOL HTTP/1.1 The Server Protocol we are using is HTTP1.1
Those are the basic info we should know.And if you want to know more about the HTTP Protocol, just find some network books to read it. I think this one is good at it.

Now, we can keep our eyes in the terminal:

 
[01/Oct/2013 08:57:26] "GET /display/ HTTP/1.1" 200 13373
[01/Oct/2013 08:57:26] "GET /static/bootstarp.css HTTP/1.1" 304 0
[01/Oct/2013 08:57:26] "GET /static/table.css HTTP/1.1" 304 0
 

From the basic log of django we can see our http request the first line means: Your browser said to the server: Hi I use the HTTP/1.1 protocol and want the content in display, just give me that!
Then, the lovely django will response it, he find the content(make the vars into the template) and give it back to your browser. The browser give the content and you will see it. And the 200 means everything is ok, you got want you want, everyone is happy.

But what is second line means? In HTTP protocol the 304 means when to deal with the static files like css js image files, the browser may store it in your last browse,if the files changed, it will download it again,if not changed just use the last version in your local file.The content is zero if nothing changed.

Well, if we just change the table.css like this and reflesh the page we have created:

 
td {
 border: 1px solid red;
}
 

From the terminal, it is the same result:

    
[01/Oct/2013 09:14:32] "GET /display/ HTTP/1.1" 200 13373
[01/Oct/2013 09:14:32] "GET /static/bootstarp.css HTTP/1.1" 304 0
[01/Oct/2013 09:14:32] "GET /static/table.css HTTP/1.1" 200 31
    

We can see that the browser request the table.css again beacuse it has been changed it the server sied.And the 13373 and 31 means the size of the file.And our table.css is 31 byte.

Now, we just do something using the terminal play as the browser,see what will happen.Just use the command :

    
zoo@ubuntu:~/桌面$ telnet localhost 8000
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
    

Type this line and Enter Button:

    
GET /display/ HTTP/1.1
    

And we also get the info from the server.Copy it to a html file and see it, we find that something is gone, such as the user_agent because we did not use it.

All right, the next post we will learn the Form in Django. And welcome to feedback if there is anything wrong in the posts.Thanks.

没有评论:

发表评论