gpt4 book ai didi

python - 我应该如何从电路框架中获取其他 URL?

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:44 25 4
gpt4 key购买 nike

应该如何从 Python 3 的 Circuits 框架 Controller 中的一个方法中获取多个 URL?这是我想要的一个简单示例,除了 urllib3。最好在开始时请求两个 URL,当它们都返回时,继续执行。

# -*- coding: utf-8 -*-
__author__ = 'jscarbor'
import urllib3
from circuits.web import Server, Controller, Static

http = urllib3.PoolManager()


class Root(Controller):
def index(self):
self.response.headers["Content-Type"] = "text/plain"
a = http.request('GET', 'https://www.w3.org/services/html2txt?url=http%3A%2F%2Fwww.example.com%2F').data
b = http.request('GET', 'http://home.hiwaay.net/~jimes/checklist.txt').data

return "%s %s" % (a, b)


(Server(8011) + Root()).run()

最佳答案

您需要使用与 Controller 不同的 channel 将 circuits.web.client.Client 组件注册到您的 Controller (因为事件名称在客户端和服务器组件中相同)。然后您可以将请求事件发送到此 channel 并等待响应。在您的特定示例中,您需要为请求事件注册一个处理程序以挂接到响应过程。我目前没有工作示例,但这是一个起点的基础:

from circuits.web.client import Client, request as request_event
from circuits.web import Server, Controller
from circuits import handler


class Root(Controller):

@handler('request')
def _on_request(self):

a = yield self.wait(request_event('GET', 'https://www.w3.org/services/html2txt?url=http%3A%2F%2Fwww.example.com%2F'), channel='url-fetching')
b = yield self.wait((request_event('GET', 'http://home.hiwaay.net/~jimes/checklist.txt'), channel='url-fetching')

self.response.headers["Content-Type"] = "text/plain"
self.response.body = "%s %s" % (a.value.read(), b.value.read())


(Server(('0.0.0.0', 8011)) + Root() + Client(channel='url-fetching').run()

关于python - 我应该如何从电路框架中获取其他 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41883179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com