Python & Go language to complete the simplest web App

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Bare-handed use of Python and go language to build the simplest Web pages-using templates, no persistence


Perhaps we will be exposed to a lot of language Web applications, such as Php,java, including the Python and go today, in fact, when we use these languages to build Web applications, many times it becomes a simple call package and API, ignoring the underlying principle.
However, all Web applications, models are consistent.
The browser sends an HTTP request;
The server receives the request, generates an HTML page, and returns it to the client as the body;
The client parses the corresponding HTML page.

By figuring out this simple logic, we can easily develop Web applications.

First, the Python version:

#!/usr/bin/env python
#-*-Coding:utf-8-*-

From flask import flask, request, render_template

App = Flask (__name__)

@app. Route ('/', methods=[' GET ', ' POST ')
Def home ():
Return render_template (' todo.html ', view= "Happy")

if __name__ = = ' __main__ ':
App.run (host= ", port=8000, Debug=true)


The first two lines of the code are comments and the code that must be included.
The first line represents this as a python executable script.
The second line means that the encoding is utf-8.

The next line represents the introduced package. Representative from the flask package inside the introduction of flask, request, render_template the three packets.
Since we are doing web applications, let me introduce flask.
https://book.douban.com/subject/26274202/
Flask relies on two external libraries: the JINJA2 template engine and the Werkzeug WSGI toolset. The JINJA2 template engine is a common template engine for Python web, similar to Smarty for PHP. WSGI is the basic tool for Python web development and does not do much to repeat.

The next line of code.
Since a single module is used here, using __name__ (value "__main__"), this line of code is equivalent to defining one, the module

The following three lines of code, including one line adorner and two rows of functions. The adorner indicates that the function is entered under the default path.
For this function, execute the template todo.html and pass the value of the view.

The last two lines represent that the module can only be executed when interpreted by the Python interpreter (avoid being a module)

Then speak using the Go language

The go language I write is more troublesome:
Package Main
Import (
"Html/template"
"Log"
"Net/http"
)

Const (
Username = "root"
Userpwd = ""
dbname = "Gooo"
)

User is User
Type User struct {
ID string
Name string
}

Presented separately, render the template
Func Render (W http. Responsewriter, tmplname string, Context map[string]interface{}) {

Tmpl: = template. New ("index.html")
tmpl,err:= template. Parsefiles (Tmplname)
Err:=nil
If err! = Nil {
http. Error (W, err. Error (), HTTP. Statusinternalservererror)
Return
}
Tmpl. Execute (W, Nil)
Return
}


Home interface
Func Indexhandler (w http. Responsewriter, R *http. Request) {
_, Err: = Getdb (username, userpwd, dbname)
If err! = Nil {
http. Error (W, err. Error (), HTTP. Statusinternalservererror)
Return
}
Locals can be used as a template variable to render, here I do not use
Locals: = Make (map[string]interface{})
Users: = []user{}
locals["users"] = Users
Render (W, "./index.html", nil)
Return
}


The connection configuration used to write the get database is not really implemented
Func getdb (username, userpwd, dbname String) (Int,error) {
Return 123,nil
}

Func Main () {
Binding
http. Handlefunc ("/", Indexhandler)
Binding port
ERR: = http. Listenandserve (": 8880", nil)
If err! = Nil {
Log. Fatal ("Listenandserve:", err.) Error ())
}
}

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.