如何将扭曲的python代码托管到互联网



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

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的域

如何更改我的代码,使其侦听www.examplecode.com而不是localhost,而不是在localhost中侦听?

您需要域指向运行此代码的服务器。当你购买域名时,你购买域名的注册商通常会给你一个设置dns配置的接口,通常你会设置一个AA或CNAME记录,指向ip或现有的其他域,指向你将用来运行代码的服务器。

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

或者,如果你将路由器配置为将440和80端口上的连接直接连接到这台电脑的本地ip,并将家庭连接的ip放入注册商DNS配置中(希望你有一个静态ip(,你可以在家里使用电脑。

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

相关内容

最新更新