Answer the question
In order to leave comments, you need to log in
Python: sockets on dynamic dns, how to implement?
I decided to implement a global socket server using dynamic dns using the no-ip.com service, configured it, checked it using ping, the ping goes, but when I try to connect via python, an error occurs:
OSError: [WinError 10049] The required address for its context is incorrect
Answer the question
In order to leave comments, you need to log in
sock.bind((socket.gethostbyname("***********.ddns.net"),0)) #host is correct
There is a network interface on your computer (roughly speaking, a network card) with the address which resolves from ***********.ddns.net? Will you accept incoming connections? After all, you bind the socket to be accessible from a specific interface for incoming (!) Connections. If you do not specify an interface, the socket will be available on all network interfaces of the computer.
Zero port can only be used if you clearly know what you are doing. All ports up to 1024 require admin rights to bind the socket.
Two conditions are required for correct operation:
1: You are binding the socket to an existing NIC interface, or to everything using "socket.bind(('', port))
2: The port you are trying to bind to the socket is not yet in use.
You do not need to use bind to connect anywhere, bind is only for listening.
port for connecting via connect is required.
import socket
sock = socket.socket()
HOST = socket.gethostbyname("***********.ddns.net")
PORT = 80
sock.connect((HOST, PORT)) #host указан верно PORT - обязательно
# sock.listen(1) - только для прослушивания
# con,addr = sock.accept() только для прослушивания
#while True: не имеет смысла, нет корректного выхода из цикла.
sock.send(b"HI")
cock.close()
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
# Echo client program
import socket
HOST = 'localhost' # адрес, куда подключаемся, localhost = 127.0.0.1
PORT = 50007 # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question