banner



How To Upload A File Using Python

SimpleHTTPServer Explained: How to Send Files Using Python

As a web developer, at that place will be a point when you demand to create your own local web server.

Maybe information technology's considering you lot'll be on a flight and want to work on your project, far from internet service. Or perchance you just want a quick way to access files from another calculator on your home network.

Whenever and however the need arises, setting up a local HTTP server is a useful skill to have.

What is an HTTP server?

Just put, an HTTP server or web server is a procedure running on a auto that listens for incoming requests and serves spider web pages.

For case, when you blazon in https://www.freecodecamp.org/news/ into your browser, there's a server somewhere listening for that request. In response, it sends back data so your browser tin render the freeCodeCamp Developer News folio.

Of form there's a lot more than happening behind the scenes, only for the purposes of this tutorial, that's all you lot really need to know.

How to prepare up a local HTTP server

  1. Install Python
  2. Open up your control prompt or final and run python -V
  3. Become to your project's directory with cd on *goose egg or MacOS systems or CD for Windows
  4. Run the following commands to start a local HTTP server:
                # If python -V returned 2.X.Ten python -m SimpleHTTPServer  # If python -V returned 3.10.Ten python3 -m http.server  # Note that on Windows yous may need to run python -k http.server instead of python3 -m http.server              

You'll notice that both commands look very different – one calls SimpleHTTPServer and the other http.server. This is simply because the SimpleHTTPServer module was rolled into Python'due south http.server in Python 3. They both piece of work the same way.

Now when you go to http://localhost:8000/ you lot should see a listing of all the files in your directory. Then you tin only click on the HTML file you want to view.

Only keep in heed that SimpleHTTPServer and http.server are only for testing things locally. They merely do very basic security checks and shouldn't be used in production.

How to transport files locally

To set upwardly a sort of quick and dirty NAS (Network Fastened Storage) system:

  1. Make sure both computers are connected through same network via LAN or WiFi
  2. Open your command prompt or final and run python -5 to make certain Python is installed
  3. Go to the directory whose file y'all want to share by using cd (change directory) command.
  4. Go to the directory with the file yous desire to share using cd on *nix or MacOS systems or CD for Windows
  5. Starting time your HTTP server with either python -thousand SimpleHTTPServer or python3 -m http.server
  6. Open new terminal and blazon ifconfig on *zero or MacOS or ipconfig on Windows to observe your IP address

Now on the 2nd calculator or device:

  1. Open browser and type in the IP address of the beginning machine, forth with port 8000: http://[ip address]:8000

A folio will open showing all the files in the directory being shared from the start figurer. If the folio is taking too long to load, you may need to conform the firewall settings on the first reckoner.


Learn to code for complimentary. freeCodeCamp'south open up source curriculum has helped more than 40,000 people get jobs every bit developers. Get started

Source: https://www.freecodecamp.org/news/simplehttpserver-explained-how-to-send-files-using-python/

Posted by: kinghossing.blogspot.com

0 Response to "How To Upload A File Using Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel