python服務器端、客戶端的模型,客服端發送請求,服務端進行響應(web.py)

服務器端、客戶端的模型,客服端發送的請求,服務端的響應

至關於啓動了一個web server

install web.py 接口框架用到的包html

http://webpy.org/tutorial3.zh-cn 官方網址python

http://webpy.org/tutorial3.zh-cnweb

 

須要裝一個web.py的包,接口就是用web.py來作的,文件上傳之類的

能夠看一下web.py的官方教程,學習一下服務器端程序時怎麼寫的

學學服務端編程的主題編程

pip install web.py服務器

Urls=()路由app

App表示路由在程序應用裏全局生效框架

類裏定義訪問跟程序的對應關係,就是路由post

def Get 表示對GET的請求作處理學習

上傳文件時,html中有<form enctype=’multipart/form-data’ action=>,multipart/form-data-代表身份,action-提交表單

Action表單元素的value所提交的網頁,用來驗證捕獲get或post的值是否正確,例如帳戶信息ui

Interface_index.py:

# -*- coding: utf-8 -*-

import web

from web import form

 

import cgi

#動態的往模板(templates是一個靜態網頁)裏填充數據

render = web.template.render('templates/')

路由,訪問跟程序的對應關係

urls = (

    #’http://127.0.0.1:8080/’默認沒加任何地址,會調用下邊的index類的GET方法

     '/', 'index',

    #’http://127.0.0.1:8080/interface’加了/interface,會調用下邊的interface類的GET或post方法

     '/interface', 'interface'

)

app = web.application(urls, globals())

 

class index:

    def GET(self):

              #return "Hello, world!"

              #print "hello world"

        #form = myform()

        # make sure you create a copy of the form by calling it (line above)

        # Otherwise changes will appear globally

         #print(form.render())

        return render.formtest()#返回模板頁面,沒有任何東西

 

 

class interface:

    def GET(self): return "hi"

    def POST(self):

         i = web.input()#是一個字典

         print 「i:」,i

         return 'form value:',i.title#把字典key名爲title的值給取到

 

if __name__=="__main__":

    web.internalerror = web.debugerror

app.run()

 

formtest.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>haha</title>

</head>

<body>

 

 

 

<form method="post" action="interface">

<p><input type="text" name="title" /> <input type="submit" value="submit" /></p>

</form>

 

</body>

</form>

</html>

 

目錄存放結構:

 

啓動web.py服務

D:\test\study>python interface_index.py

http://0.0.0.0:8080/

 

 

訪問http://127.0.0.1:8080/沒加任何路徑,訪問index,就是返回一個頁面

 

訪問/interface路徑,訪問GET

http://127.0.0.1:8080/interface

 

 

訪問post,在http://127.0.0.1:8080/頁面,輸入內容,點擊submit後自動進行post訪問,頁面會調到http://127.0.0.1:8080/interface

# -*- coding: utf-8 -*-

import web

from web import form

import cgi

 

render = web.template.render('templates/')

#路由

urls = (

     '/', 'index',

     '/interface', 'interface'

)

app = web.application(urls, globals())

 

class index:

    def GET(self):

              #return "Hello, world!"

              #print "hello world"

        #form = myform()

        # make sure you create a copy of the form by calling it (line above)

        # Otherwise changes will appear globally

         #print(form.render())

        return render.formtest()

 

class interface:

    def GET(self): return "hi"

    def POST(self):

         i = web.input()

         print "i:",i

         return 'form value:',i.title,i #form value是字符串

 

if __name__=="__main__":

    web.internalerror = web.debugerror

app.run()

 

index頁面輸入helloxia點擊submit

 

後臺:

 

i: <Storage {'title': u'helloxia'}>

127.0.0.1:49925 - - [18/Jun/2018 22:36:05] "HTTP/1.1 POST /interface" - 200 OK

我提交的字段的名字做爲key,值做爲value

 

這就是介紹了一下服務器端是怎麼工做的

相關文章
相關標籤/搜索