加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

使用httplib,进行POST请求

(2009-08-13 00:08:43)
标签:

it

分类: 编程知识
def query(self):
        try:
            url = httplib.urlsplit(self.url)
            conn = httplib.HTTPConnection(url.netloc)
            conn.connect()

            #conn.set_debuglevel(1)
            if self.data:
                conn.putrequest("POST", self.url)
                conn.putheader("Content-Length", len(self.data))
                conn.putheader("Content-Type", "application/x-www-form-urlencoded")
            else:
                conn.putrequest("GET", self.url, None)
                conn.putheader("Content-Length", 0)

            conn.putheader("Connection", "close")
            conn.endheaders()

            if self.data:
                conn.send(self.data)

            f = conn.getresponse()
            if f:
                self.response = f.read()
                f.close()
            conn.close()
---------------------------------------------
import urllib
import httplib

headers = {'Accept-Language':'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.0)','Connection':' Keep-Alive',
        }
params = ({key:value, ...})#此处的key:value为你请求url时所需要的参数

conn = httplib.HTTPConnection('www.google.cn')
conn.request('GET', '/search', params)
res = conn.getresponse()
data = res.read()

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有