Итак, я пытаюсь создать сервер, к которому я могу обращаться за пределами его собственной сети, поэтому я пытаюсь работать с общедоступными IP-адресами. Однако когда я пытаюсь подключить их, я получаю сообщение об ошибке:
ConnectionRefusedError: [WinError 10061] Соединение не может быть установлено, поскольку целевой компьютер отказался подключаться. Вот мой код:
Сервер (здесь я использую частный IP-адрес, потому что в противном случае возникает ошибка OSError: [WinError 10049] Запрошенный адрес недействителен в этом контексте):
Код: Выделить всё
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
with SimpleXMLRPCServer(('172.30.1.217', 8000),allow_none=True,
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x + y
server.register_function(adder_function, 'add')
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'mul').
class MyFuncs:
def mul(self, x, y):
return x * y
def message(self,message,sender):
with open('chat.txt','a') as file:
file.write(sender+": "+message+"\n")
def openchat(self):
with open('chat.txt','r') as file:
s=""
for line in file:
s+=line.strip()+"\n"
return s
def clear(self,partly=True):
s=[]
with open('chat.txt','r') as file:
for line in file:
s.append(line.strip())
if partly:
with open('chat.txt','w') as file:
file.write(s[len(s)-2]+"\n"+s[len(s)-1]+"\n")
else:
with open('chat.txt','w') as file:
file.write("")
def getchatlength(self):
with open('chat.txt','r') as file:
i=0
for line in file:
i+=1
return i
server.register_instance(MyFuncs())
# Run the server's main loop
server.serve_forever()
Код: Выделить всё
import xmlrpc.client
import tkinter.messagebox as tk
import os.path as o
import select
import sys
import keyboard
import time
from playsound3 import playsound
def loadlength():
with open("infoChat.txt","r") as f:
for line in f:
return int(line)
def inclength():
länge=loadlength()+1
with open("infoChat.txt","w") as f:
f.write(str(länge))
def writelength(length):
with open("infoChat.txt","w") as f:
f.write(str(length))
s = xmlrpc.client.ServerProxy('http://178.190.81.229:8000')
if o.isfile("infoChat.txt")==False:
with open("infoChat.txt","w") as f:
length=s.getchatlength()
if length>0:
print("Neue Nachricht")
playsound('audio.mp3')
f.write(str(length))
while True:
länge = loadlength()
while True:
if keyboard.is_pressed('alt'):
break
time.sleep(0.01)
if länge
Подробнее здесь: [url]https://stackoverflow.com/questions/79192909/xmlrpc-server-connecting-to-end-devices-outside-of-its-own-network[/url]
Мобильная версия