【问题标题】:How to host twisted python code to the Internet?如何将扭曲的 Python 代码托管到 Internet?
【发布时间】:2021-09-03 19:12:31
【问题描述】:

目前我有以下代码,它简单地回显发送到服务器的数据,并向客户端显示服务器中的活动连接数并发送一些信息。

from twisted.internet.protocol import Protocol , Factory
from twisted.internet import reactor
from twisted.internet.endpoints import TCP4ServerEndpoint


connections = -1

class echo_simple(Protocol):
    def connectionMade(self):
            global connections
            connections += 1
            self.transport.write(f'Number of active connections in the server are: {connections} '.encode('utf-8'))
    def connectionLost(self,*args , **kwargs):
        global connections
        connections -= 1
        print(':: N :: A Connection Just Closed :: N ::')

    def dataReceived(self, data):
        data = data.decode('utf-8')
        print(f':C: {data} ')
        self.transport.write("\n Server Successfully Received Your Message!".encode('utf-8'))
        self.transport.write(f"\n |THE MESSAGE YOU SENT IS : {data}|".encode('utf-8'))
        self.transport.write(f'\n Closing The Connection As This is Just An Echo Server'.encode('utf-8'))
        self.transport.loseConnection()


class Server_factory(Factory):
    def buildProtocol(self, addr):
        print(" |^INFO^| Created An Instance ")
        return echo_simple()



endpoint = TCP4ServerEndpoint(reactor, 8009)
endpoint.listen(Server_factory())
reactor.run()
 

我的问题是服务器目前正在 LocalHost 中侦听,我想在全球 internet 中托管它!

例如,我有一个名为 www.examplecode.com

的域

如何更改我的代码而不是在 localhost 中监听,以便它监听 www.examplecode.com 而不是 localhost

【问题讨论】:

    标签: server protocols twisted endpoint python-3.9


    【解决方案1】:

    您需要域指向运行此代码的服务器。 当您购买域名时,您购买它的注册商通常会为您提供一个设置 dns 配置的界面,通常您会设置一个 AA 或 CNAME 记录以指向指向您将使用的服务器的 ip 或现有的其他域运行代码。

    您通常会从另一家公司租用一台服务器(其中有很多台),该服务器将在数据中心运行并得到维护、连接到该服务器并部署您的代码在它上面(通常通过 ssh),并让它在 0.0.0.0 上侦听,以便它接受来自任何地方的连接。然后,您获取此服务器的 IP,并使用它在您的注册商提供的界面中设置 DNS。

    或者,如果您将路由器配置为将 440 和 80 端口上的连接直接连接到这台 PC 的本地 ip,并且您将家庭连接的 ip 放在注册商 DNS 配置中,您也可以在家中使用计算机(但希望你有一个静态 IP)。

    无论如何,这对于 stackoverflow 问题来说有点宽泛(而且它甚至可能不是 stackoverflow 的主题,也许它更像是一个服务器故障问题,或者是一个超级用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 2010-12-10
      相关资源
      最近更新 更多