我如何用扭曲的方式运行克莱因? [英] How do I run Klein with twisted?

查看:40
本文介绍了我如何用扭曲的方式运行克莱因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用扭曲运行 klein,因此我可以在不同路径上运行扭曲脚本(exp:example.com/example1example.com/example2).所以我做了一个简单的脚本:

I'm trying to run klein with twisted, so I can run twisted scripts on different paths (exp: example.com/example1, example.com/example2). So I made a simple script:

from klein import run, route, Klein
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.python import log

@route('/example')
def home(request):
    site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, b''))
    reactor.listenTCP(80, site)
    reactor.run()

run("My_IP_Address", 80)

但是每当我运行这个脚本时,我都会收到一个错误:twisted.internet.error.CannotListenError:无法监听任何:80:[Errno 98] 地址已经在使用中.克莱因非常新,我不确定它是如何工作的,有人能告诉我我做错了什么吗?谢谢!

But whenever I run this script I get an Error: twisted.internet.error.CannotListenError: Couldn't listen on any:80: [Errno 98] Address already in use. I'm very new to Klein, and I'm not sure how it works, Could anybody tell me what it is I'm doing wrong? thanks!

推荐答案

你得到的这个异常似乎很清楚它说:

This exception that you're getting seems fairly clear it says:

Couldn't listen on any:80: [Errno 98] Address already in use.

当您尝试使用的端口号已被某些其他服务使用时,就会发生这种情况.此其他服务可以是 Twisted 或两个 Twisted 服务以外的其他服务.我将假设您没有在端口 80 上侦听任何其他内容(例如 nginx 或 apache 或其他一些 Web 服务器,请注意 80 是默认的 HTTP 端口,因此许多服务可以在那里侦听)并且您的问题是由启动两个引起的扭曲的网络服务.

it happens when port number you're trying to use is already used by some other services. This other service can be either something other than Twisted or two Twisted services. I'm going to assume you dont have anything else listening on port 80 (e.g. nginx or apache or some other web server, note that 80 is default HTTP port so many services can be listening there) and that your problem is caused by starting two twisted web services.

在您的情况下,您正在尝试启动两个服务在一个端口上侦听.

In your case you are trying to start two services listening on one port.

run("My_IP_Address", 80)

启动一项监听 80 端口的服务.

starts one service listening on port 80.

在/example 路由上收到请求后,您尝试在同一端口上启动另一个服务:

After receiving request on /example route you're trying to start another service on this same port:

site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, b''))
reactor.listenTCP(80, site)
reactor.run()

这没有逻辑意义,您不能在同一个端口上运行两个服务.这就是您收到此异常的原因.此外,您对 reactor.run() 的调用也没有用,从 klein 导入的 run() 已经启动了 reactor.

this does not make logical sense, you can't have two services running on same port. This is why you get this exception. Also your call to reactor.run() is useless, run() imported from klein already starts reactor.

如果您确实需要在某个请求之后启动某个服务器(这似乎是非常不寻常的用例),请在不同的端口上启动它.但也许您应该从官方文档和示例开始?

If you really do need to start some server after some request (this seems like very unusual use case) start it on a different port. But maybe you should simply start with official documentation and examples there?

这篇关于我如何用扭曲的方式运行克莱因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆