zoukankan      html  css  js  c++  java
  • python get post模拟请求

    1.使用get方式时。url相似例如以下格式:

    1. index.jsp?

      id=100&op=bind  


    GET报问头例如以下:

    1. GET /sn/index.php?

      sn=123&n=asa HTTP/1.1  

    2.  Accept: */*  
    3.  Accept-Language: zh-cn  
    4.  host: localhost  
    5.    
    6.   
    7.  Content-Type: application/x-www-form-urlencoded  
    8.  Content-Length: 12  
    9.  Connection:close  


     

    2.使用post方式时。POST方法将请求參数封装在HTTP请求数据中,以名称/值的形式出现,能够传输大量数据,可用来传送文件。

    POST报文头例如以下:

    1. POST /sn/index.php HTTP/1.1  
    2. Accept: */*  
    3. Accept-Language: zh-cn  
    4. host: localhost  
    5.   
    6. Content-Type: application/x-www-form-urlencoded  
    7. Content-Length: 12  
    8. Connection:close  
    9. sn=123&n=asa  


    在http头后边有一空行,空行后边接着发送post数据。空行通知server下面不再有请求头。

     

    3.能够发现的一点是,不管是post还是get方式,他们所传递的数据都要进行url编码

     

    4. url编码是一种浏览器用来打包表单输入的格式。

     浏览器从表单中获取全部的name和当中的值 ,将它们以name/value參数编码(移去那些不能传送的字符。将数据排行等等)作为URL的一部分或者分离地发给server

     不管哪种情况,在server端的表单输入格式样子象这样:

      theName=Ichabod+Crane&gender=male&status=missing& ;headless=yes

     

    5.URL编码遵循下列规则:

         1.每对name/value由&。符分开。

         2.每对来自表单的name/value由=符分开。

         3.假设用户没有输入值给这个name,那么这个name还是出现,仅仅是无值。

         4.不论什么特殊的字符(就是那些不是简单的七位ASCII。如汉字)将以百分符%用十六进制编码

    6.所以。当我们使用get或者post传送数据之前,我们都须要对数据进行url编码。

       urllib库提供了一个函数来实现url的编码:

    1. search=urllib.urlencode({'q':'python'})  

    输出为:

    1. 'q=python'  

     

    7.ok,如今正式開始python的get和post请求:

    1. #!/usr/bin/python      
    2. #-*-coding:utf-8-*-      
    3.        
    4. # 进行表单提交  小项  2008-10-09      
    5.        
    6. import httplib,urllib;  #载入模块      
    7.        
    8. #定义须要进行发送的数据      
    9. params = urllib.urlencode({'cat_id':'6',       
    10.                            'news_title':'标题-Test39875',       
    11.                            'news_author':'Mobedu',       
    12.                            'news_ahome':'来源',       
    13.                            'tjuser':'carchanging',       
    14.                            'news_keyword':'|',       
    15.                            'news_content':'測试-Content',       
    16.                            'action':'newnew',       
    17.                            'MM_insert':'true'});       
    18. #定义一些文件头      
    19. headers = {"Content-Type":"application/x-www-form-urlencoded",       
    20.            "Connection":"Keep-Alive","Referer":"http://192.168.1.212/newsadd.asp?

      action=newnew"};       

    21. #与站点构建一个连接      
    22. conn = httplib.HTTPConnection("192.168.1.212");       
    23. #開始进行数据提交   同一时候也能够使用get进行      
    24. conn.request(method="POST",url="/newsadd.asp?

      action=newnew",body=params,headers=headers);       

    25. #返回处理后的数据      
    26. response = conn.getresponse();       
    27. #推断是否提交成功      
    28. if response.status == 302:       
    29.     print "公布成功!^_^!";       
    30. else:       
    31.     print "公布失败^0^/";       
    32. #关闭连接      
    33. conn.close();    
    另外通过urllib2

     

               url=/game/user/info?id=28

               full_url='http://192.168.1.250'+url.

               data=urllib2.urlopen(full_url)
               Data=data.read()


查看全文
  • 相关阅读:
    poj 1654(利用叉积求面积)
    poj 3230(初始化。。动态规划)
    hdu 1392(凸包)
    hdu 1348(凸包)
    hdu 1147(线段相交)
    hdu 1115(多边形重心问题)
    POJ 2373 Yogurt factory
    GCJ 2008 APAC local onsites C Millionaire
    FZU 1397 保送
    FZU 1064 教授的测试
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10828390.html
  • Copyright © 2011-2022 走看看