gpt4 book ai didi

python - 如何给twisted服务器一个IP地址?

转载 作者:行者123 更新时间:2023-12-01 08:53:35 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的客户端/服务器(其中客户端和服务器不是同一台机器),但无法弄清楚如何给出扭曲的 IP 地址。

from twisted.internet import protocol, reactor, endpoints
from twisted.protocols import basic

class FingerProtocol(basic.LineReceiver):
def lineReceived(self, user):
self.transport.write(self.factory.getUser(user) + b"\r\n")
self.transport.loseConnection()

class FingerFactory(protocol.ServerFactory):
protocol = FingerProtocol

def __init__(self, users):
self.users = users

def getUser(self, user):
return self.users.get(user, b"No such user")

fingerEndpoint = endpoints.serverFromString(reactor, ("192.168.1.7", "tcp:1079"))
fingerEndpoint.listen((FingerFactory({ b'moshez' : b'Happy and well'})))
reactor.run()

错误:

Traceback (most recent call last):
File "sensors/twistedTest.003.py", line 20, in <module>
fingerEndpoint = endpoints.serverFromString(reactor, ("192.168.1.7", "tcp:1079"))
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/endpoints.py", line 1724, in serverFromString
nameOrPlugin, args, kw = _parseServer(description, None)
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/endpoints.py", line 1635, in _parseServer
args, kw = _parse(description)
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/endpoints.py", line 1596, in _parse
for (type, value) in _tokenize(description):
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/endpoints.py", line 1570, in _tokenize
current += n
TypeError: can't concat bytes to tuple

如何给twisted/reactor一个IP地址?

最佳答案

不要使用 ("192.168.1.7", "tcp:1079") 尝试 b"tcp:1079:interface=192.168.1.7"。

像这样:

fingerEndpoint = endpoints.serverFromString(reactor, b"tcp:1079:interface=192.168.1.7")

此外,地址“192.168.1.7”应该是服务器计算机上某些网络接口(interface)的地址。

关于python - 如何给twisted服务器一个IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52942549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com