【发布时间】: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