问题
I have created a chat application using python sockets and Tkinter and it all works perfectly locally however the Client is unable to connect to the server remotely (when I enter my public IP address as the host) I have already fully port-forwarded my network and I know how to port forward very well and when I run an online Port Open scanner that checks if a port is open it states that the port is open!?
I have port-forwarded my router on a number of ports and updating the client and server accordingly however the client and telnet could still not connect.. I have also disabled all my windows firewalls and I disabled all of the routers firewalls.
Here is a very simple socket client and server model which I am trying to troubleshoot my problem using.
Server
import socket
s = socket.socket()
host='0.0.0.0'
port = 2000
s.bind((host,port))
s.listen(5)
while True:
c, addr = s.accept()
print ("Got a connection from: ", addr)
c.send(bytes("Thanks for connecting",'utf8'))
c.close()
Client
import socket
s = socket.socket()
port = 2000
s.connect(("109.156.114.183", port))
print (s.recv(1024))
s.close
Telnet
C:\Users\Maks>telnet 109.156.114.183 2000
Connecting To 109.156.114.183...Could not open connection to the host, on port 2000: Connect failed
When I try to connect to the server on 127.0.0.1 or localhost or from within my local network the client connects perfectly and telnet can connect as well. I am 200% sure I have port forwarded correctly because when I run a Port Open scan it says that the port(2000) is open.
Please help! Thanks- Maks
回答1:
I'm at home so there's no firewalls running on my network at all.
That comment was the missing piece to solve your problem. You actually don't connect from remote as your question implies but you try to connect from inside your local network to the externally visible address of your router.
Such a setup is supported by some routers and not by others. It looks like you router does not support it. For more information on this see NAT hairpinning (or NAT loopback).
来源:https://stackoverflow.com/questions/56310901/cant-remotely-connect-to-python-socket