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. September 8th 2016 Week 37th Thursday

    The secret of high-impact business is early preparation. 高效商务,赢在未雨绸缪. Early and best preparation is ...

  2. ArrayList 和 LinkedList 的区别

    1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.2.对于随机访问get和set,ArrayList优于LinkedList,因为LinkedList要移动 ...

  3. cascade 介绍与用法 ( oracle)

    级联删除,比如你删除某个表的时候后面加这个关键字,会在删除这个表的同时删除和该表有关系的其他对象 1.级联删除表中的信息,当表A中的字段引用了表B中的字段时,一旦删除B中该字段的信息,表A的信息也自动 ...

  4. Eclipse的使用及Java程序的标识符和关键字

    Eclipse的使用 (1)创建Java项目 选择“文件”/“新建”/“Java项目”命令,在弹出的“新建Java项目”对话框输入项目名,然后点击“下一步”,最后单击“完成”. (2)创建Java类文 ...

  5. mysql 查看用户的权限

    show grants for 'username'@'%';

  6. CLR via C#(18)——Enum

    1. Enum定义 枚举类型是经常用的一种“名称/值”的形式,例如: public enum FeedbackStatus     {         New,         Processing, ...

  7. linux中who命令显示的tty、pts和(:0)(:0.0)是什么意思

    http://blog.csdn.net/cwj_beyond/article/details/6987345 http://unix.stackexchange.com/questions/7217 ...

  8. static_cast、dynamic_cast、reinterpret_cast、const_cast以及C强制类型转换的区别

    static_cast 1. 基础类型之间互转.如:float转成int.int转成unsigned int等 2. 指针与void*之间互转.如:float*转成void*.CBase*转成void ...

  9. Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

    MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...

  10. RSA 加解密算法

    与DES不同,RSA算法中,每个通信主体都有两个钥匙,一个公钥一个私钥. 就是有2把钥匙1.使用publicKey可以对数据进行加密2.使用Key才能对数据进行解密单方向传输用公钥加密的数据,只有私钥 ...