1. [代码]GET 方法

import httplib

#-----------------------------

conn = httplib.HTTPConnection("www.python.org")

conn.request("GET", "/index.html")

r1 = conn.getresponse()

print r1.status, r1.reason

06 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 

06 200 OK

data = res.read()

print len(data)

09 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

09 200 OK

data = response.read()

conn.close()a

转载:python发送HTTP请求的更多相关文章

  1. python发送post请求上传文件,无法解析上传的文件

    前言 近日,在做接口测试时遇到一个奇葩的问题. 使用post请求直接通过接口上传文件,无法识别文件. 遇到的问题 以下是抓包得到的信息: 以上请求是通过Postman直接发送请求的. 在这里可以看到消 ...

  2. Python发送http请求时遇到问题总结

    1.报错信息为“ERROR 'str' object has no attribute 'endwith'”,排查发现endswith方法名写错了,少了s,写成了 'endwith' if inter ...

  3. python发送网络请求

    1.使用urllib模块(使用不方便,建议使用第二种) get请求: res = urlopen(url) from urllib.request import urlopen url = 'http ...

  4. python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息

    背景介绍: 发送搜索请求时,需要用到登录接口返回值中的token值 代码实现: 登录代码: 搜索接口:

  5. requests模块--python发送http请求

    requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...

  6. python发送post请求

    urllib2.urlopen() urlib2是使用各种协议完成打开url的一个扩展包.最简单的使用方式是调用urlopen方法,比如 def urlopen(url, data=None, tim ...

  7. python 发送POST请求

    #博客地址:https://blog.csdn.net/qq_36374896 import urllib.request import urllib.parse url = "http:/ ...

  8. python 发送GET请求

    # #博客地址:https://blog.csdn.net/qq_36374896 # 特点:把数据拼接到请求路径的后面传递给服务器 # # 优点:速度快 # # 缺点:承载的数据量小,不安全 imp ...

  9. 转载---HttpUrlConnection发送post请求汉字出现乱码的一个解决方法及其原因

    原文:http://blog.csdn.net/qqaazz211/article/details/52136187 在网上看到了这篇比较简单的解决方法,果然有用,特记之 解决方法是:将 out.wr ...

随机推荐

  1. net 页面跳转

    前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...

  2. java课后作业5

    [问题]随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中. 设计思路: 1.申请一个长度为10的数组 2.计算机随机生成10个数,并赋给数组 3. ...

  3. maven配置httpclient3.X jar包

    <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging& ...

  4. iOS小技巧总结,绝对有你想要的

    原文链接 在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIV ...

  5. html+css+js实现标签页切换

    CSS部分: #Tab { margin:0 auto; width:640px; border:none; position:absolute; z-index:9; margin-left:320 ...

  6. iOS 十六进制和字符串转换

    NSString *dictString = [dict JSONFragment];//组合成的. dictString==={"content":"Sadgfdfg& ...

  7. [转] C++的STL库,vector sort排序时间复杂度 及常见容器比较

    http://www.169it.com/article/3215620760.html http://www.cnblogs.com/sharpfeng/archive/2012/09/18/269 ...

  8. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  9. route 一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机

    一个很奇怪的现象:我的主机能ping通同一网段的其它主机,并也能xshell 远程其它的主机,而其它的主机不能ping通我的ip,也不能远程我和主机. [root@NB Desktop]# route ...

  10. HTML5 – 2.新元素

    figcaption 定义和用法 <figcaption> 标签定义 figure 元素的标题(caption). "figcaption" 元素应该被置于 " ...