Why does socket.gethostbyname(socket.gethostname) binds only to local ip address? - python

I am writing a socket program to create a simple server.
When I write ip = socket.gethostbyname(socket.gethostname()) and then I print ip it prints 127.0.1.1
Why does this keep happening?
My device is connected to a mobile hotspot connection still the ip address remains of a local host.
I am using ubuntu 19.04 OS

The problem is that a host has multiple interfaces. It is not a problem is you use a true DNS or a carefully handwritten /etc/host file because then the system will look there to find the translation. But depending on the configuration, the host name can be bound to all the available interfaces, including the loopback one. And gethostbyname returns the address of the first of those interfaces in its own order.
To make sure of that, you should use gethostbyname_ex which returns a list of all the interfaces, and you should find the hostspot connected one, in addition to the loopback one.

Related

How can I create a TCP connection in Python between 2 PCs

So far I have made a VERY basic client/server application that creates a TCP connection. I have a lot of programming experience, just never did this low-level stuff and especially nothing with networks. Note that all the prints are just to help me figuring out what is going on. One of the known issues is that jsonip sometimes gives me an IPv4 and sometimes v6, I don't know why but that doesn't matter for now, just to warn anyone who wants to recreate my code.
Server:
import socket
import requests
port = int(input("Enter port you want to open:\n"))
#todo: add errorhandling
print("Adding socket...")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
print(f"Hostname: {hostname}")
ip_address = socket.gethostbyname(hostname)
print(f"Host address: {ip_address}")
r = requests.get(r'http://jsonip.com')
public_ip_address = r.json()['ip']
s.bind((ip_address, port))
print("Is open for connections on IP: "+public_ip_address+" and Port: "+str(port))
s.listen(5)
print("Done initialisation, listening for incoming connections...")
while True:
clientsocket, address = s.accept()
print(f"Connection from {address} has been established")
clientsocket.send(bytes(f"You have connected to server: {hostname}", "utf-8"))
Client:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = input("Enter IP to connect to:\n")
port = int(input("Enter Port to connect to:\n"))
print(f"Connecting to server {ip} ...")
s.connect((ip, port))
msg = s.recv(1024)
print(msg.decode("utf-8"))
On my local machine: Open 20000 in my server.py, it tells me the host is 127.0.1.1, I then enter 127.0.1.1 into my client script and 20000, and they connect. So the Socket has been bound with the 127.0.1.1. (Side question: What is this IP address, is it like the internal IP address of processes in my PC or something? If running ip a on my other machine it is the first one shown of 2)
Using Virtmanager on my machine and running one Linux Server (command line only) and one normal Ubuntu, the server tells me the host is, again, 127.0.1.1 which I don't need to enter into the other VM to know it won't work, what does work however, is getting the IP-address of the Server via ip a, which in this case is 192.168.122.37, and when I enter this IP address into the client, it connects. But in the socket here I bind, again, the 127.0.1.1, so is it arbitrary what I put here? What SHOULD I bind here, the public, the weird or the 192. address?
The first thing I could not get to work was using 2 physical devices. When opening a server on my Linux machine, I cannot connect with my windows machine at all, no matter if I use my public, my 127. or my 192. IP-address. Now my end goal is doing this over the internet so I am walking myself up, describing here the steps I took to try and get where I want to be but here I hit a brick wall where I don't know what is wrong. Am I binding the wrong address on the server, is my router being a problem, is there something else wrong?
I also tried leaving my network using my friends pc a few countries over, but this also just results in a timeout (my theory is that the Router port he is trying to open is closed and I have now idea how I can make the router send data to his PC, which should be not impossible as firefox and every application using internet does it without me having to manually forward every port, I just don't know how). This is my end goal, creating a connection between my friends PC and mine, and this is how far I got (I wouldn't mind skipping the local network if it is not relevant for fixing the global connection problem), so, tl;dr: what did i do wrong, what do i need to bind and what do i need to do for the final result to work?
There are many questions to answer.
Addresses 127.X.X.X are reserved for the loopback interface, most common one is 127.0.0.1. The loopback is a virtual, but important interface and as you have probably guessed, it is usable on the local machine only. You cannot use 127.X.X.X address to make two hosts to communicate with each other.
Addresses 192.168.X.X (and also 10.X.X.X and 172.16-31.X.X.) are reserved for local LANs. They are not valid on the Internet.
You cannot use these addresses to make two hosts to communicate with each other over the public Internet (unless you create a tunnel, an advanced networking topic)
Almost everybody uses them, because we ran out of IPv4 addresses long time ago, they were difficult to get, expensive, etc. Also such hosts are isolated from the Internet, they can be reached only via a router that translates addresses. Such router feature is called NAT. A typical router has one valid Internet address and all connections to the Internet appear as coming from the router. If you contant a service like jsonip.com from a PC, you get your router's address, not your PC's address.
See also: Finding local IP addresses using Python's stdlib
To make your program working, make it to accept connections on all interfaces. See the first example in the socket docs. On Linux, use port numbers >= 1024. Ports < 1024 are reserved, not available to regular users.
Final point is that a firewall may prevent connections to your server. It depends on your system and setup.

Python sockets google dns server

I need my own IP in a small script and in order not to hardcode it, I`ve found a piece of code from here(stackoverflow) that works.
This--
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
print(s.getsockname()[0])
--
What is not clear for me is why it only works on UDP and not TCP? It has something to do with the google dns server? Thanks in advance.
This has nothing to do with Google and nothing to do with DNS.
All what this code does is to "connect" a UDP socket to an external IP, so that the OS kernel figures out which local IP address it needs to use in order to reach this external system. This is no real connection, i.e. there is no traffic involved but the OS kernel is only checking routing tables and local interfaces in order to decide which IP address to use as source in case one would actually use the socket to send data.
One could do the same with TCP. But in this case a real TCP connection would be established which means that actual traffic would be exchanged and that the connect would fail if the external system would not be reachable on this port (i.e. no listener, firewall in between etc).
With UDP instead connect will not produce any traffic and would fail only if no route to the destination IP address could be determined. This also means that an arbitrary external IP address and port could be used, i.e. ('1.1.1.1',11) would work the same as ('8.8.8.8',80).

How to lookup host name from IPv6 address with Python

How do you lookup the local hostname from an IPv6 address in Python?
I'm trying to diagnose network bandwidth hogs, and I'm using Wireshark to find which IPv6 addresses are using the most bandwidth, but it doesn't include any hostsnames, so I'm left with a CSV of IPv6 addresses and total bytes transmitted.
Note, I'm not talking about converting a generic IP to domain, since that only works for DNS, not your local network's hostnames.
The only command line tool I know for doing this is:
sudo nmap -sn 192.168.1.0/24
but that doesn't list IPv6 addresses.
This is what I found looking online, I'm sorry but i think that what you are lookin at isn't possible.
From WireShark Q&A:
What you're looking at are probably neighbor discovery packets, which
are sent to a special multicast address called "solicited node
multicast".
It's basically the replacement mechanism for ARP, which is not used
for IPv6 anymore.
Usually, if you need to find out where something is coming from you
should try to determine the MAC address of the source, and then log in
to your switches to find the port where that MAC address is connected.
There should be a command that will show you the MAC address table of
the switch. If you can find a port where only the MAC address in
question is listed you need to follow the cable from that port to the
device.
If you have more than one MAC address listed for a port it is usually
a connection to another switch. In that case you need to log in to the
switch it connects to and repeat your search.

Sending a http get request to a server with known public ip

I have a server running by using python's base http server. The host name used is '127.0.0.1' the local host, and the port number is set to 8000. I have the public ip address of the computer operating this server.
If I wanted to send a http get request to this from another computer, what would I type into my browser?
Sounds like you've got your server process running on the wrong interface. 127.0.0.1 is not a hostname but an IP address, specifically the local loopback address. It is not reachable from any other machine (unless something's gone tragically wrong with your network configuration).
You can run anything you like on the 127.0.0.1 interface, and no one else can directly connect to it from a remote machine. That's pretty much the point --- it's for testing programs that use the Internet Protocol, and (in recent years) for starting single-user servers without worrying about security. (Python 2's SimpleHTTPServer does this, as do some personal wikis, and I think iPython Notebook.)
The public address for the host running your Web server is a completely unrelated network interface, with its own hardware and its own port 8000. It doesn't know or care that you've got something listening on some other interface's port 8000, so it should refuse attempts to connect to that port.
Since you didn't post any code, I have no idea what you need to change to get your server running on the correct interface. Assuming you've more or less followed the example in the BaseHTTPServer.HTTPServer docs:
def run(
server_class=BaseHTTPServer.HTTPServer,
handler_class=BaseHTTPServer.BaseHTTPRequestHandler,
):
server_address = ('', 8000) # <----= Replace the string.
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
That server_address tuple is a string containing the IP address ('1.2.101.202' or whatever), followed by an integer port number. So replace the string with your host machine's public-facing IP address.
Note that port 8000 is outside the reserved range (0 up to but not including 1024), so it's possible that some unrelated service is already using that port. (Numerous applications are already squatting port 8000.) If so, you'll just have to choose another port number. You can chose anything from 1024 up to but not including 65536, but as with 8000, someone else might already be using it.
Depending on your operating system and its security setup, you might not have permission to open a socket that listens on an arbitrary port number. If so, that's between you and your ISP or sysadmin.
http://yourip:port/func
yourip is your public ip.
port is 8080
func is your registered function.
and also make sure you port is opened

Why doens't IPv6 display client IP name like IPv4 does?

Hi there sorry for disturbing you guys. But I read something in the Magazine about porting codes to IPV6 so i tried just changing this code (socket.AF_INET6, socket.SOCK_STREAM)
In python it works, but the problem is that the server does not display the client ip.
I expected it to display it like this. Got connection from ('127.0.0.1', 59815).
But for it , it messes up all thing and give me this (Got connection from ('::1', 59815, 0, 0)),
So if I ask were is the client IP here?. and what does the two last zeros(0, 0) really mean?. And what should i do to port my code to IPv6.? Thanks iam using python(geany)
So if I do this(socket.socket(socket.AF_INET6, socket.SOCK_STREAM)) will I have put my code to IPV6, o are there some other things I need to do thanks)
::1 is the IPv6 address for localhost, just like 127.0.0.1 is the IPv4 address for localhost. So you are seeing the client's IPv6 address.
The fields in the socket address are documented in the Python socket documentation. They are:
host
port
flowinfo
scope-id
Flowinfo and scope-id are new for IPv6. Flowinfo contains the Flow Label, which is specified in RFC 6437. If you don't know how to use it you can safely leave it at 0. The scope-id is used when an address is valid in multiple scopes. IPv6 link-local addresses for example are valid on every IPv6 interface, but routing them from one interface to another is not possible. So if you want to communicate with link-local addresses you have to specify which interface to use. The scope-id is the number of the interface.
When porting code to IPv6 you have to think about a few things. The socket handling you already have changed. You should also look at name resolving and use functions that support both IPv4 and IPv6 (i.e. don't use gethostbyname() but use getaddrinfo() etc.) And don't forget to allocate enough space when storing IP addresses. IPv6 addresses take up more space than IPv4 addresses.
PS: The IPy library provides very useful classes for dealing with IP addresses.

Categories

Resources