Python: socket to dynamic dns, how to implement?

I decided to implement a socket server on a dynamic dns service no-ip.com set up, tested using ping ping is, but when you try to connect via python error:
OSError: [WinError 10049] the Requested address to your context wrong
Code
import socket
sock = socket.socket()
sock.bind((socket.gethostbyname("***********.ddns.net"),0)) #host is correct
sock.listen(1)
con,addr = sock.accept()
while True:
sock.send(b"HI")
sock.close()

PS connect is also not working
June 7th 19 at 15:43
1 answer
June 7th 19 at 15:45
Solution
sock.bind((socket.gethostbyname("***********.ddns.net"),0)) #host is correct

On Your computer there is a network interface (roughly speaking, a network card) to an address that will resolvida of ***********.ddns.net? You will accept incoming connections? After you bind the socket to the availability with a particular interface for incoming(!) connections. If you do not specify the interface - socket is available on all network interfaces of the computer.

A null port can only be used if You clearly know what you are doing. All ports up to 1024 require administrator privileges to bind a socket.

To work correctly you need two conditions:
1: You bind a socket to an existing network interface, or on all if you use "socket.bind((", port))
2: the port that You are trying to bind to a socket that nobody else uses.

To connect anywhere the bind is not necessary to use, bind is only for listening.
the port to connect via connect - mandatory.

import socket
sock = socket.socket()
HOST = socket.gethostbyname("***********.ddns.net")
PORT = 80
sock.connect((HOST, PORT)) #host is correct and PORT - mandatory
# sock.listen(1) - only for listening
# con,addr = sock.accept() listen-only
#while True: does not make sense, there are no proper exit from the loop.
sock.send(b"HI")
cock.close()


Note that sock.accept() operation is blocking, i.e. the program will wait for an incoming connection before it happens.

An example from the documentation:
# 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' # the address where you are connecting to, 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))

Find more questions by tags PythonSocketsDynDNS