How to connect to a Localhost in linux - python

I want to do a connection between a computer simulating being a server and another computer being a user, both with Linux.
In the first computer I've created a directory called "server" and in that directory I've done the following command:
python3 -m http.server 8080
Then I can see that directory going to the localhost. But what I want is to see that localhost from the other computer, I tried with wget, and the gnome system of sharing files but none of them worked, and I'm not seeing any solution online.

I'm not sure I fully understand your question but if you want to reach your folder from an other computer with your command python3, you can use the option -b followed by an IP address used on your linux.

All you need to do is get the IP of the server using ifconfig command.
ifconfig | grep 192 make sure not to use the broadcast address part.
Now logon to the other machine and hit the command
http://192.168.71.145:8080
And it should work.
Here I have assumed that your IP is 192.168.71.145 and the port is 8080.
Just change the IP and port as per your values.

If I'm understanding your question correctly you want to connect to the server from another machine on the same network.
If so you can run hostname -I on the server to output to the local IP address of the server, you can then use this on the other machine to connect to it (provided they are on the same network)

Related

How to connect Linux Virtual Machine from its host computer?

I'm creating a simple chat app using Python.
The server code is in the Linux VM (I am using Virtual Box), and the client code is in the Windows 10 computer where the Virtual Box is installed.
I'm trying to connect it with the Python socket.
It works when I ping both machines to each other.
My problem is what port should I put in the client code:
Client
import socket
import subprocess
cliente = socket.socket()
try:
cliente.connect(('192.168.1.33',9090))
cliente.send("1")
I tried every port available; however, nothing works. I think I am missing something to make this work.
First, You need to make sure that the port number passed to bind function in server code in Linux VM is the same port number used by connect function in your client.
Check this simple server-client example in python which is using port 12345 :
https://www.tutorialspoint.com/python/python_networking.htm
Second, (based on your comments) the IP address that should be used in client connect function is the IP address of the machine running the server code which is in your case the Linux VM. Try to run the shell command ifconfig in the Linux VM to get the IP address.
If this address didn't work you can change the network settings of the virtual machine to bridged instead of NAT and try again the ifconfig command and get the new IP address.
Doing SSH from the host machine to Linux Virtual machine will work great!!

How do I access a python http server from a remote connection?

I'm trying out the command python http.server from the command line, and am trying to access the server. I can access easily from localhost, but whenever I try to use a remote connection, I am unable to connect.
I've tried different ports, and it doesn't look like my firewall is blocking any connections.
From the command line, I run
python3 -m http.server 8000
which returns
Serving HTTP on 0.0.0.0 port 8000 ...
However, I can only connect to the server from localhost.
sudo iptables -S
returns the following:
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
I expect to be able to connect from a remote host with the url http://10.247.30.125:8000 (my ip address), but I am unable to do so. The same url works fine on localhost.
I figured out what was going on. Did some reading about IP and NAT, and noticed that any ip address beginning with a 10 is reserved for private ip addresses. So, trying to connect to 10.247.30.125:8000 from anywhere other than my own network (so while not connected to my wifi) doesn't work. To be able to connect from an external network, I would need to set up port forwarding (like Reedinationer suggested), and I would need to use my router's public IP address rather than my computer's private IP address. I would set up a port forward from my router to direct external traffic to my computer, which would allow me to connect to my personal computer from an external network.
Thanks to everyone who responded!
port is missing from the url try this url it should work "http://10.247.30.125:8000"

How do I host a localhost on my server with Mac Terminal?

I've been hosting a localhost on my Mac with CSS, HTML, and JS. To do this i just navigate to my file with cd Desktop followed by cd filename , and then I do python -m SimpleHTTPServer 8000 to host my server on my localhost. I know that this only works for the person hosting the server, but I'd like to host it on my local network, so anyone that goes to localhost:8000 will see it. (I'm fine with it not being localhost:8000, in fact, I'd love a custom name.)
Thank you
-A
First of all, localhost is a "domain" name if you like. Most of the times it resolves to 127.0.0.1 which is the loopback ip address(e.g. points back to your computer).
I am going to assume you are using python 2.x
So here we go:
#!/usr/bin/env python
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
addr = ("0.0.0.0", 8000) #host to everyone
serv = BaseHTTPServer.HTTPServer(addr, SimpleHTTPRequestHandler)
serv.serve_forever()
Save that to a python script and run with:
python myfile.py
If you are using python 3 then go with :
python3 -m http.server --bind 0.0.0.0 8000
Now for someone else to access your server through your local network, you have to give them your machine's ip.
To do that run:
ifconfig |grep inet
You should get something alone the lines of:
inet 192.168.1.2 netmask 0xffffff00 etc etc
Now anyone on your local network can use your server by typing
192.168.1.2:8000
in their browsers
One easy way to expose localhost to other people is using ngrok, available via brew install ngrok or that link.
In your example above, running ngrok 8000 would allow other people to access the server you've got hosted. It's worth noting, obviously, that this isn't restricted to your local network!
Perhaps a better option (if all you're doing is hosting static HTML, CSS & JS) would be to set up a simple Apache instance. The default config should probably work just fine, and people could then access your page using your computer's local IP e.g 192.168.0.10:8000, or whatever port you set up.
EDIT: As the other answerer pointed out, SimpleHTTPServer will do everything Apache does... You just need to give people your machine's local IP!

Cannot set up local server using external IP

I'm trying to set up two servers on my laptop using the script in https://github.com/misheska/foundations-of-python-network-programming/blob/master/python2/02/udp_remote.py.
As far as I understand, I can set a server just by typing
$ python udp_remote.py server
I want to start another server using my external IP, that I get using:
$ wget -q -O - http://myexternalip.com/raw
XXX.XXX.XXX.XXX
Then the server should be set with
$ python udp_remote.py server XXX.XXX.XXX.XXX
right???
Your syntax is correct (other than your IP4s being only 3 bytes long), but there are a couple reasons why you're having trouble:
It's not possible to bind to the same port twice on the same interface. Because the script has a fixed port number, you won't be able to run more than one instance unless your laptop has multiple interfaces.
Your WAN IP address will only be resolvable to an interface if your laptop itself is actually assigned that address. If the laptop is connected to a router and is assigned a local address, you won't be able to use the WAN address to specify the interface.
You cannot bind to an IP address that is not bound to an interface on the computer. If you require port forwarding to be set up on a router then consult your network administrator.

manage.py runserver

I am running python manage.py runserver from a machine A
when I am trying to check in machine B. The url I typed is http://A:8000/ .
I am getting an error like The system returned: (111) Connection refused
You can run it for machines in your network by
./manage.py runserver 0.0.0.0:8000
And than you will be able to reach you server from any machine in your network.
Just type on other machine in browser http://192.168.0.1:8000 where 192.168.0.1 is IP of you server... and it ready to go....
or in you case:
On machine A in command line ./manage.py runserver 0.0.0.0:8000
Than try in machine B in browser type http://A:8000
Make a sip of beer.
Source from django docs
You need to tell manage.py the local ip address and the port to bind to. Something like python manage.py runserver 192.168.23.12:8000. Then use that same ip and port from the other machine. You can read more about it here in the documentation.
I was struggling with the same problem and found one solution. I guess it can help you. when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000. 127.0.0.0 is the same as localhost which can be accessed locally. to access it from cross origin you need to run it on your system ip or 0.0.0.0. 0.0.0.0 can be accessed from any origin in the network.
for port number, you need to set inbound and outbound policy of your system if you want to use your own port number not the default one.
To do this you need to run server with command python manage.py runserver 0.0.0.0:<your port> as mentioned above
or, set a default ip and port in your python environment. For this see my answer on
django change default runserver port
Enjoy coding .....
Just in case any Windows users are having trouble, I thought I'd add my own experience. When running python manage.py runserver 0.0.0.0:8000, I could view urls using localhost:8000, but not my ip address 192.168.1.3:8000.
I ended up disabling ipv6 on my wireless adapter, and running ipconfig /renew. After this everything worked as expected.
in flask using flask.ext.script, you can do it like this:
python manage.py runserver -h 127.0.0.1 -p 8000
For people who are using CentOS7, In order to allow access to port 8000, you need to modify firewall rules in a new SSH connection:
sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp
sudo firewall-cmd --reload
I had the same problem and here was my way to solve it:
First, You must know your IP address.
On my Windows PC, in the cmd windows i run ipconfig and select my IP V4 address. In my case 192.168.0.13
Second as mention above: runserver 192.168.0.13:8000
It worked for me.
The error i did to get the message was the use of the gateway address not my PC address.
First, change your directory:
cd your_project name
Then run:
python manage.py runserver
Ok just came across this post this is a little off topic but hopefully explains a few things, The IP 127.0.0.1 points to your network card so any traffic that you cause to go to that IP address will not leave your computer.
For example modern network cards in laptops for example will not even give you that IP if you are not connected to a wifi or cabled network so you'll need to be connected at least to activate the card.
If you need to run multiple servers on the same machine but want to access them with a domain then you have a couple of options
edit your computers host file to define the domain and what IP it goes to
use a DNS Alias I set up using a cname record years ago *.local.irishado.com will point to 127.0.0.1
so for example these three domains will point to your local machine
http://site1.local.irishado.com
http://site2.local.irishado.com
http://site3.local.irishado.com
will all point to your local machine then in python projects you will need to edit the projects setting file ALLOWED_HOSTS property to hold the domain it will accept
ALLOWED_HOSTS = ['site1.local.irishado.com']

Categories