• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python发送HTTP请求的方法小结

python发送HTTP请求的方法小结

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python发送http请求,python 发送http post,python httpresponse,python3 http,http://python.org等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python发送HTTP请求的方法。分享给大家供大家参考。具体如下:

这里包含 Python 使用 GET/HEAD/POST 方法进行 HTTP 请求

1. GET 方法:

>>> 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()

</div>

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

</div>

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()

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python搭建HTTP服务器和FTP服务器
  • Python实现HTTP协议下的文件下载方法总结
  • Python发送form-data请求及拼接form-data内容的方法
  • python发送HTTP请求的方法小结

相关文章

  • python的else子句使用指南
  • 用Python将IP地址在整型和字符串之间轻松转换
  • 用Python的Flask框架结合MySQL写一个内存监控程序
  • Python中对象迭代与反迭代的技巧总结
  • 简单掌握Python的Collections模块中counter结构的用法
  • 深入解析Python中的lambda表达式的用法
  • 在Python中操作字典之setdefault()方法的使用
  • python实现字符串连接的三种方法及其效率、适用场景详解
  • Python中的条件判断语句与循环语句用法小结
  • Python中使用bidict模块双向字典结构的奇技淫巧

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python 爬虫的工具列表大全
    • Python去掉字符串中空格的方法
    • Python 深入理解yield
    • Python中使用PIPE操作Linux管道
    • Python多层嵌套list的递归处理方法(推荐)
    • 简单掌握Python的Collections模块中counter结构的用法
    • Python的requests网络编程包使用教程
    • 发布你的Python模块详解
    • 用Python操作字符串之rindex()方法的使用
    • 巧用Python装饰器 免去调用父类构造函数的麻烦

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有