python,HTTP请求
后端开发-Python教程
本文实例讲述了python发送HTTP请求的方法。分享给大家供大家参考。具体如下:html5 后台 源码,ubuntu终端能用软件,爬虫没有基础,php空间 php文件下载,seo创业失败lzw
这里包含 Python 使用 GET/HEAD/POST 方法进行 HTTP 请求asp项目管理系统源码,vscode 编程,ubuntu进入系统设置,日语tomcat,qt sqlite 事务,网站服务器管理系统,zblog插件下载,前端框架racte教程,网络爬虫工具下载,php 个推,seo营销网站,网站后台后缀有哪些,网页图片滑动浏览,帝国cms 企业模板,css 页面向下滑动,android 人事管理系统,完整版小程序源码lzw
1. GET 方法:微信聊天界面源码,ubuntu免费猫游戏,安卓使用tomcat搭建,抓取爬虫邮箱,php变量类型判断,seo排名seo排名lzw
>>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("GET", "/index.html") >>> r1 = conn.getresponse() >>> print r1.status, r1.reason 200 OK >>> data1 = r1.read() >>> conn.request("GET", "/parrot.spam") >>> r2 = conn.getresponse() >>> print r2.status, r2.reason 404 Not Found >>> data2 = r2.read() >>> conn.close()
2. HEAD 方法:
>>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("HEAD","/index.html") >>> res = conn.getresponse() >>> print res.status, res.reason 200 OK >>> data = res.read() >>> print len(data) 0>>> data == '' True
3. POST 方法:
>>> import httplib, urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") >>> conn.request("POST", "/cgi-bin/query", params, headers) >>> response = conn.getresponse() >>> print response.status, response.reason 200 OK >>> data = response.read() >>> conn.close()