`
jashuang
  • 浏览: 17637 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Python HTTPConnection example

阅读更多
18.7.3 Examples
Here is an example session that uses the "GET" method:


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

Here is an example session that shows how to "POST" requests:


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

分享到:
评论

相关推荐

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    Python - http.client实现短信验证码注册登录完整示例:Python - http.client.py和附件说明.rar

    conn = http.client.HTTPConnection("api.example.com") conn.request("POST", "/send-sms", {"phone": "1234567890"}) response = conn.getresponse() data = response.read().decode() conn.close() ``` 在上述...

    Python标准库示例

    本书《Python标准库示例》(The Python Standard Library by Example)由资深Python专家Doug Hellmann编写,属于开发者图书馆系列的一部分。该系列书籍旨在为软件开发人员提供最新的编程语言和技术方面的高质量参考...

    python爬虫入门request 常用库介绍整理.docx

    conn = http.client.HTTPConnection("example.com") conn.request("GET", "/") res = conn.getresponse() data = res.read() print(data.decode("utf-8")) ``` ##### 4. **aiohttp** - **简介**:`aiohttp`...

    深入理解Python3中的http.client模块

    >>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10) ``` `source_address`参数是在Python 3.2版本中添加的,而`strict`参数在3.4版本中被移除。 2. **HTTPSConnection** `...

    对python3标准库httpclient的使用详解

    conn = http.client.HTTPConnection("www.example.com") ``` ### 设置头部信息 HTTP请求头可以包含很多信息,如用户代理、编码格式等。在Python中,可以通过`set_request_header`方法设置头部信息: ```python ...

    Python爬虫实现HTTP网络请求多种实现方式

    conn = http.client.HTTPConnection('www.baidu.com') conn.request('GET', '/') response = conn.getresponse() html = response.read().decode('utf-8') conn.close() print(html) ``` 以上就是Python爬虫...

    python实现多线程网页下载器

    conn = httplib.HTTPConnection(url) conn.request('GET', '/', headers=HEADERS) response = conn.getresponse() data = response.read() # 这里可以处理下载到的数据,例如保存到文件等 conn.close() def ...

    python中requests小技巧

    http.client.HTTPConnection.debuglevel = 1 response = requests.get('http://httpbin.org/get') print(response.text) ``` #### 技巧7:使用grequests实现异步请求 对于高并发场景,使用`grequests`库可以帮助...

    httplib:http解析

    conn = httplib.HTTPConnection("www.example.com") ``` 2. **发送请求**:然后,你可以调用`conn.request()`方法来发送HTTP请求。它接受四个参数:请求方法(如"GET"或"POST")、URL、请求头(可选)和请求体...

    ahttplib-开源

    conn = ahttplib.HTTPConnection("example.com") conn.request("GET", "/path/to/resource") response = conn.getresponse() print(response.status, response.reason) data = response.read() conn.close() ``` ...

Global site tag (gtag.js) - Google Analytics