python get post模拟请求

来源:互联网 发布:java导出至excel表格 编辑:程序博客网 时间:2024/05/04 12:05

1.使用get方式时,url类似如下格式:

[html] view plaincopy
  1. index.jsp?id=100&op=bind  


GET报问头如下:

[html] view plaincopy
  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报文头如下:

[html] view plaincopy
  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数据。空行通知服务器以下不再有请求头。

 

3.可以发现的一点是,无论是post还是get方式,他们所传递的数据都要进行url编码

 

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

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

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

  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的编码:

[html] view plaincopy
  1. search=urllib.urlencode({'q':'python'})  

输出为:

[html] view plaincopy
  1. 'q=python'  

 

7.ok,现在正式开始python的get和post请求:

[html] view plaincopy
  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()


1 0
原创粉丝点击