从0开始python后端开发_前端(html)向后端(python)传递数据_GET_POST

转:https://blog.csdn.net/bjbz_cxy/article/details/79358718


关于POST和GET的请求方法和区别请参考:HTTP协议下GET与POST的区别

GET方法

首先先使用html简单编写一个表单页面

代码如下:

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>GET方法传递数据(runoob.com)</title>  
  6. </head>  
  7. <body>  
  8. <form action="/cgi-bin/backend_get.py" method="get">  
  9. 数据值1: <input type="text" name="data_1">  <br />  
  10. 数据值2: <input type="text" name="data_2" />  
  11. <input type="submit" value="GET提交" />  
  12. </form>  
  13. </body>  
  14. </html>  

运行示列:


后端python代码:

[plain]  view plain  copy
  1. backend_get.py  
[python]  view plain  copy
  1. #!/usr/bin/python  
  2. # -*- coding: UTF-8 -*-  
  3.   
  4. # 添加GI处理模块  
  5. import cgi, cgitb   
  6.   
  7. # 创建FieldStorage的实例化  
  8. form = cgi.FieldStorage()   
  9. #获取html页面传递过来的数据值  
  10. str_data_1  =  form.getvalue('data_1')  
  11. str_data_2  =  form.getvalue('data_2')  
  12. #打印输出  
  13. print "Content-type:text/html"  
  14. print  
  15. print "<html>"  
  16. print "<head>"  
  17. print "<meta charset=\"utf-8\">"  
  18. print "<title>GET</title>"  
  19. print "</head>"  
  20. print "<body>"  
  21. print "<h2>data_1:%s,data_2:%s</h2>" % (str_data_1, str_data_2)  
  22. print "</body>"  
  23. print "</html>"  


写完之后别忘记给python脚本加上运行权限

别忘记sudo

[plain]  view plain  copy
  1. sudo chmod 755 backend_get.py  

运行示列:

前端:


请求url:

[html]  view plain  copy
  1. /cgi-bin/baeckend_get.py?data_1=test_1&data_2=test_2  

后端输出:

[python]  view plain  copy
  1. data_1:test_1,data_2:test_2  

POST方法:

表单代码只需要copy一份然后修改method属性值改为post即可

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>POST方法传递数据(runoob.com)</title>  
  6. </head>  
  7. <body>  
  8. <form action="/cgi-bin/hello_get.py" method="post">  
  9. 数据值1: <input type="text" name="data_1">  <br />  
  10. 数据值2: <input type="text" name="data_2" />  
  11. <input type="submit" value="POST提交" />  
  12. </form>  
  13. </body>  
  14. </html>  

后端代码无需变更直接copy一份即可,将名字改为backend_post,py,无需给权限,copy会连同文件权限一并复制!

[python]  view plain  copy
  1. #!/usr/bin/python  
  2. # -*- coding: UTF-8 -*-  
  3.   
  4. # 添加GI处理模块  
  5. import cgi, cgitb   
  6.   
  7. # 创建FieldStorage的实例化  
  8. form = cgi.FieldStorage()   
  9. #获取html页面传递过来的数据值  
  10. str_data_1  =  form.getvalue('data_1')  
  11. str_data_2  =  form.getvalue('data_2')  
  12. #打印输出  
  13. print "Content-type:text/html"  
  14. print  
  15. print "<html>"  
  16. print "<head>"  
  17. print "<meta charset=\"utf-8\">"  
  18. print "<title>POST</title>"  
  19. print "</head>"  
  20. print "<body>"  
  21. print "<h2>data_1:%s,data_2:%s</h2>" % (str_data_1, str_data_2)  
  22. print "</body>"  
  23. print "</html>"  

运行示列:


url:

[html]  view plain  copy
  1. /cgi-bin/baeckend_get.py  

后端输出

[python]  view plain  copy
  1. data_1:test_1,data_2:test_2  

猜你喜欢

转载自blog.csdn.net/m0_37870649/article/details/80759318