我如何允许远程访问我的 python API?
How i allow remote access to my python API?
我构建了一个 python RESTful API,它正在使用 Klein/Twisted。我不想使用本地主机,而是希望允许访问我的 public IP 地址,这样每个人都可以连接到我的 API。 (这是临时测试,直到我将它托管在 AWS EC2 上)。
我一直很迷茫如何做到这一点,并遵循了诸如转发端口 80 之类的教程,但它似乎不起作用。如果我的知识有限,我深表歉意,因为我在这个领域不是很精通。有人可以给我指点资源吗?
这是我要更改为我的 public IP 和端口 80 的代码,以便可以访问我的 API。
endpoint_description = "tcp:port=%s:interface=localhost" % 8090
endpoint = endpoints.serverFromString(reactor, endpoint_description)
IPv4/TCP 和 IPv6/TCP 端点的默认设置是侦听所有接口。你要做的就是不通过指定接口来限制它:
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
endpoint = TCP4ServerEndpoint(reactor, 8090)
另请注意,您的示例中无需进行字符串混合。只需实例化您想要的端点并将其传递给您想要的参数即可。字符串语法用于根据仅支持字符串(CLI、配置文件等)的系统的描述创建端点。
我构建了一个 python RESTful API,它正在使用 Klein/Twisted。我不想使用本地主机,而是希望允许访问我的 public IP 地址,这样每个人都可以连接到我的 API。 (这是临时测试,直到我将它托管在 AWS EC2 上)。
我一直很迷茫如何做到这一点,并遵循了诸如转发端口 80 之类的教程,但它似乎不起作用。如果我的知识有限,我深表歉意,因为我在这个领域不是很精通。有人可以给我指点资源吗?
这是我要更改为我的 public IP 和端口 80 的代码,以便可以访问我的 API。
endpoint_description = "tcp:port=%s:interface=localhost" % 8090
endpoint = endpoints.serverFromString(reactor, endpoint_description)
IPv4/TCP 和 IPv6/TCP 端点的默认设置是侦听所有接口。你要做的就是不通过指定接口来限制它:
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
endpoint = TCP4ServerEndpoint(reactor, 8090)
另请注意,您的示例中无需进行字符串混合。只需实例化您想要的端点并将其传递给您想要的参数即可。字符串语法用于根据仅支持字符串(CLI、配置文件等)的系统的描述创建端点。