Server

How To: Start A Quick Python Server

Today’s article is going to show you how to transfer files between computers by using a quick Python server. It’s remarkably easy! It’s a temporary server (usually) and lasts only as long as you need it to.

Why would you do this? Well, you can transfer files from one computer to another. It also functions as an HTTP server which makes it easy to test things like simple web pages quickly and easily.

Are there better options? Quite probably. If you want to transfer a single file, then SCP is a good way to go about it. If you want to transfer multiple files, you could setup SFTP. If you want to test web pages, you can likely just write the files locally and then open them up with the browser of your choice.

You have options! And, thankfully, Linux provides all sorts of options – including setting up a quick Python server. As I said, it’s actually pretty easy.

A Quick Python Server:

This article requires an open terminal, like many other articles on this site. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

The first command I want you to run will let us know what version of Python you have installed. Many distros have (at present) moved onto version 3, but some still have Python 2 installed. So, for that information you just run:

python -V

Now, if you have Python v. 2.x, you would use this command:

python -m SimpleHTTPServer

If you’re using Python v. 3.x then the command you’d use is:

python3 -m http.server

(If you’re curious, the -m is telling Python which module to open.)

Anyhow,  you can now connect to your server with the following command:

ip.address.of.desktop:8000

Read how to find your IP address. Instead of an IP address, you can also use your hostname.

Anyhow, you  now have a server running on port 8000. If you want to, you can also change the port number. This is the same for both commands. In both cases, just append your chosen port number to the command. Like this:

python3 -m http.server 9000

It’d look a little like this:

Note the changed port number. You should probably avoid reserved ports.

When you’re done with the quick Python server, you can just close it by pressing CTRL + C. If you’re planning on running it long term, you can always run the command with nohup. If applicable, you may also need to open the port in your firewall.

Like I said, it’s a quick and easy server in Python. You definitely wouldn’t want to use this as a public facing server, but it’s fine for quick tasks. Feel free to leave a comment letting folks know how you use this in  your day-to-day tasks.

Closure:

There you have it, another article said and done! The site is going well and the schedule seems to be working well enough. It’s a bit demanding to write one every other day, but that’s what I said I’d try to do. So far, so good!

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

1 day ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

3 days ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

5 days ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

7 days ago

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

1 week ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

2 weeks ago