server - 如何将扭曲的 Python 代码托管到 Internet?
问题描述
目前我有以下代码,它简单地回显发送到服务器的数据,并显示服务器中向客户端的活动连接数并发送一些信息。
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()
我的问题是服务器目前正在本地主机中侦听,我想在全球互联网上托管它!
例如,我有一个名为www.examplecode.com的域
而不是在 localhost 中收听,如何更改我的代码以使其收听www.examplecode.com而不是localhost?
解决方案
您需要域指向运行此代码的服务器。当您购买域名时,您购买它的注册商通常会为您提供一个设置 dns 配置的界面,通常您会设置一个 AA 或 CNAME 记录以指向指向您将使用的服务器的 ip 或现有的其他域运行代码。
您通常会从另一家公司租用一台服务器(有很多),它将在数据中心运行并得到照顾,连接到该服务器,并在其上部署您的代码(通常通过 ssh),然后它在 0.0.0.0 上侦听,因此它接受来自任何地方的连接。然后,您获取此服务器的 IP,并使用它在您的注册商提供的界面中设置 DNS。
或者,您可以在家中使用计算机,如果您将路由器配置为将 440 和 80 端口上的连接直接连接到这台 PC 的本地 ip,并且您将家庭连接的 ip 放在注册商 DNS 配置中(希望您虽然有一个静态IP)。
无论如何,对于stackoverflow问题来说,这是一个广泛的主题(它甚至可能不是stackoverflow的主题,也许它更像是一个服务器故障问题,或者一个超级用户,一个。
推荐阅读
- python - 如何在numpy数组中找到最大非无穷大值的索引?
- javascript - 三.js统一虚线相对于相机
- ios - 在 App Center Distribute 中禁用身份验证
- java - WebView.loadData 在 Android 9.0 (API-29) 上不起作用
- elasticsearch - Logstash REST 过滤器 - 限制速度
- java - android-text 视图中的字母空间
- bash - 如何在文件bash中循环打开和结束字符串?
- terraform - 使用 Terraform 创建 azurerm_virtual_machine 资源时,有什么方法可以避免 storage_os_disk 继承标签?
- ms-access - 访问 - 字段验证规则 - 限制为 1 种语言
- java - NoClassDefFoundError:Maven 模块间依赖