flask get, post access method

* Import flask from 

'' ' 
server to store parameters GET args request object with the flask, the get method 
to obtain the parameter, i.e. with flask.request.args.get (parameter) to obtain the value of the parameter 
' '' 
App the Flask = (name__ __) 
@ app.route ( '/') 
DEF index (): 
    the try: 
        name = request.args.get ( "name") IF "name" in the else request.args.get "" 
        Age = Request. args.get ( "Age") IF "Age" in the else request.args.get "" 
        return name + "," + Age 
    the except Exception AS ERR: 
        Print (ERR) 

IF __name__ __ == '__ main__': 
    app.run (Port = 5000, debug = True)

  

 

import  urllib.parse
import  urllib.request
url="http://127.0.0.1:5000"
try:
    #如果传参有汉字需要使用urllib.prase.quote()
    name=urllib.parse.quote("XXXX")
    age=urllib.parse.quote("二十")
    data="name="+name+"&age="+age
    html=urllib.request.urlopen("http://127.0.0.1:5000?"+data)
    html=html.read()
    html=html.decode()
    print(html)
except Exception as err:
    print(err)

  

 

 

import urllib.request
import urllib.parse

url="http://127.0.0.1:5000"
name="XXXXXXX"
age="21"
note="post传值实验。这是我的post传值实验"

name=urllib.parse.quote(name)
age=urllib.parse.quote(age)
note=urllib.parse.quote(note)

data="name="+name+"&age="+age+"&note="+note

resp=urllib.request.urlopen(url,data=data.encode())
data=resp.read()
html=data.decode()

print(html)

  

 

 

 

from flask import *
#服务器端
app=Flask(__name__)
@app.route("/",methods=["GET","POST"])
def index():
     try:
          name=request.form.get("name") if "name" in request.form.get else ""
          age=request.form.get("age") if "age" in request.form.get else ""
          note=request.form.get("note") if "note" in request.form.get else ""
          print(name)
          print(age)
          print(note)
          data=name+"\n"+age+"\n"+note
          return data
     except Exception as err:
          print(err)


if __name__=='__main__':
    app.run(port=5000,debug=True)

  

 

A small reptiles

import urllib.request
import urllib.parse
'''
小型爬虫地址
'''
url="http://127.0.0.1:5000"
html=urllib.request.urlopen(url)
html=html.read()
html=html.decode()
print(html)

  

<! DOCTYPE HTML> 
<HTML lang = "EN"> 
<head> 
    <Meta charset = "UTF-. 8"> 
    <title> Experimental </ title> 
</ head> 
<body> 
<h1 of> Welcome python web flask frame </ h1> 
<the p-> 
    this is a test of my frame of a reptile and a flask 
</ the p-> 
</ body> 
</ HTML>

  

 

Guess you like

Origin www.cnblogs.com/byczyz/p/11116879.html