On which host do I need to bind a listening socket?

I used the python socket module and tried to open a listening socket using

import socket import sys def getServerSocket(host, port): for r in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = r try: s = socket.socket(af, socktype, proto) except socket.error, msg: s = None continue try: s.bind(sa) s.listen(1) except socket.error, msg: s.close() s = None continue break if s is None: print 'could not open socket' sys.exit(1) return s 

If the host was None and the port was 15000.

Then the program will accept connections, but only from connections on one computer. What do I need to do to accept Internet connections?

+4
source share
3 answers

Try 0.0.0.0. This is what is mainly used.

+8
source

The first problem is that your blocks prevent swallowing errors without generating reports. The second problem is that you are trying to bind to a specific interface, not to INADDR_ANY. You are probably attached to "localhost", which will only accept connections from the local machine.

INADDR_ANY is also known as the constant 0x00000000, but the macro is more significant.

Assuming you are using IPv4 (that is, the "normal Internet" these days), copy the socket / binding code from the first example into the socket module page .

+5
source

You will need to bind to a public IP address or "all" addresses (usually denoted by "0.0.0.0").

If you are trying to access it through a network, you may also need to consider firewalls, etc.

+1
source

Source: https://habr.com/ru/post/1311054/


All Articles