2

i was wondering if I could connect to a socket without knowing the server's IP address, see the example:

Server:

from socket import *

s = socket(AF_INET, SOCK_STREAM)
s.bind(("", 8080))
s.listen(1)

conn, addr = s.accept()

EDIT: That did the trick How to make a server discoverable to LAN clients

Community
  • 1
  • 1
Kava
  • 93
  • 1
  • 2
  • 9

3 Answers3

0

Just pass in "" for the client...

s = socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("", 8080))

...and you'll be able to connect to the server.

(Obviously they both have to be running on the same machine for this to work)

lafuzz
  • 74
  • 5
0

I'm not sure what your goal is, but normally for a TCP client socket, you need an IP address for the server you wish to connect to.

Why do you want to avoid specifying an IP address?

user1277476
  • 2,871
  • 12
  • 10
0

To put it short:

NO

The one thing you could do would be going through all IP addresses (Which would be only 4228250625 for IPv4, 274941996890625 for IPv6) and checking if they are listening on port 8080. The problem with this is that it would

  • take up a huge amount of time and
  • there is most likely more than one server listening on port 8080.

So, no.

jazzpi
  • 1,399
  • 12
  • 18