python - Python 将 urllib3.response.HTTPResponse 转换为 httplib.HTTPResponse
问题描述
我正在尝试从 httplib 迁移到 urllib3。urllib3.PoolManager
返回urllib3.response.HTTPResponse
而httplib.HTTPConnection
返回httplib.HTTPResponse
。
import SocketServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
import threading
import httplib
import urllib3
class S(SimpleHTTPRequestHandler):
def html(self):
content = "<html><body><h1>Hi</h1></body></html>"
return content.encode("utf8")
def do_GET(self):
self.wfile.write(self.html())
def run_server(handler_class=S, addr="localhost", port=8000):
httpd = SocketServer.TCPServer((addr, port), handler_class)
print('Starting httpd...')
httpd.serve_forever()
t = threading.Thread(target=run_server)
t.setDaemon(True)
t.start()
http = httplib.HTTPConnection("localhost", 8000)
http.request('GET', '/')
r = http.getresponse()
print str(r), r.status, r.read()
http = urllib3.connectionpool.HTTPConnection("localhost", 8000)
http.request('GET', '/')
r = http.getresponse()
print r, r.status, r.read()
http = urllib3.PoolManager()
r = http.request('GET', 'http://localhost:8000/')
print r, r.status, r.data
输出
Starting httpd...
<httplib.HTTPResponse instance at 0x10cc01c68> 200 <html><body><h1>Hi</h1></body></html>
<httplib.HTTPResponse instance at 0x10cc03b48> 200 <html><body><h1>Hi</h1></body></html>
<urllib3.response.HTTPResponse object at 0x10bfa3f10> 200 <html><body><h1>Hi</h1></body></html>
我正在处理多个调用者期望的遗留代码库httplib.HTTPResponse
。有人可以让我知道有没有办法httplib.HTTPResponse
在使用时获得,urllib3.PoolManager
或者是否有一个转换器可以用来转换urllib3.response.HTTPResponse
,httplib.HTTPResponse
以便我可以将更改最小化到几个基类?
解决方案
推荐阅读
- caching - 基准指令缓存影响
- java - 在 Thorntail 中实现 MicroProfile Config 的可移植性问题
- python - 删除列表中的特定项目
- confluence - How to parse rest api code from confluence page
- java - 用 JavaFX 中另一个打开的窗口中的对象填充 ListView
- python - 是否有可能创建一个位于所有其他窗口之上的消息对话框?
- sql - Import Excel Data Into Temporary Table Without Using OLEDB in SQL Server
- java - 如何在 Eclipse 中修复此错误“错误:无法找到或加载主类”?
- function - Propagate Execution Status
- php - 在 Windows 任务调度程序上运行的 PHP 中使用 ftp 函数有什么问题吗?