Lotus
Lotus

Reputation: 147

Why can't people connect to this "server" made with python sockets?

import socket

host, port = socket.gethostbyname(socket.gethostname()), 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)


while True:
    conn, addr = s.accept()
    print("<IP-LOGGED> " + addr[0])
    r = conn.recv(1024)
    print(r.decode("utf-8"))

    response = """
HTTP/1.1 200 OK

logger

"""
    conn.sendall(bytes(response, "utf-8"))
    conn.close()

PS: The code is not mine it's from a youtube IP grabber tutorial. And no I'm not a script kiddie I'm just trying to understand the concept of grabbing an IP / making a web server with sockets with an example. I just started learning about sockets

Upvotes: 0

Views: 72

Answers (2)

Adrien Kaczmarek
Adrien Kaczmarek

Reputation: 550

This server is on your local network. That means, more or less, that someone connected to the internet via the same router as your computer will be able to access your server by searching for <YOUR-IP-ADDRESS>:<PORT>.

However, your server is not connected to the Internet, thus someone from outside will not be able to connect to your server. If you what so, you can "buy" a server online (you can have a look at https://www.heroku.com for example, but there are plenty of them)

Upvotes: 1

CherryDT
CherryDT

Reputation: 29092

When I run socket.gethostbyname(socket.gethostname()) on my machine I get 127.0.0.1. If this is the case for you too, then you are binding only to the localhost interface, so you can only access your server from your own computer.

To allow connections through any (IPv4) interface, you need to use 0.0.0.0:

host, port = '0.0.0.0', 5555

Then, you can also connect to your server from a different computer in your network (or even over the Internet if your router is configured to forward port 5555 to your machine).

Upvotes: 0

Related Questions