As a web developer, there will be a point when you need to create your own local web server.
作为Web开发人员,您需要创建自己的本地Web服务器。
Maybe it's because you'll be on a flight and want to work on your project, far from internet service. Or perhaps you just want a quick way to access files from another computer on your home network.
也许是因为您将要飞行并且想要从事项目工作,而远离互联网服务。 或者,也许您只是想要一种快速的方法来从家庭网络中的另一台计算机访问文件。
Whenever and however the need arises, setting up a local HTTP server is a useful skill to have.
无论何时,只要有需要,设置本地HTTP服务器都是一项有用的技能。
(What is an HTTP server?)
Simply put, an HTTP server or web server is a process running on a machine that listens for incoming requests and serves web pages.
简而言之,HTTP服务器或Web服务器是在计算机上运行的进程,用于侦听传入的请求并提供网页。
For example, when you type 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 can render the freeCodeCamp Developer News page.
例如,当您在浏览器中输入https://www.freecodecamp.org/news/
,某处有一个服务器在侦听该请求。 作为响应,它将发送回数据,以便您的浏览器可以呈现freeCodeCamp开发人员新闻页面。
Of course there's a lot more happening behind the scenes, but for the purposes of this tutorial, that's all you really need to know.
当然,幕后还有很多事情要做,但是就本教程而言,这就是您真正需要知道的全部。
(How to set up a local HTTP server)
- Install Python 安装Python
- Open your command prompt or terminal and run
python -V
打开命令提示符或终端并运行python -V
- Go to your project's directory with
cd
on *nix or MacOS systems orCD
for Windows
转到您的项目目录与cd
在* nix或者MacOS系统或CD
的Windows - Run the following commands to start a local HTTP server:
# If python -V returned 2.X.X
python -m SimpleHTTPServer
# If python -V returned 3.X.X
python3 -m http.server
# Note that on Windows you may need to run python -m 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 just because the SimpleHTTPServer
module was rolled into Python's http.server
in Python 3. They both work the same way.
您会注意到,这两个命令看起来非常不同–一个调用SimpleHTTPServer
,另一个调用http.server
。 这仅仅是因为SimpleHTTPServer
模块已在Python 3中集成到Python的http.server
中。它们都以相同的方式工作。
Now when you go to http://localhost:8000/ you should see a list of all the files in your directory. Then you can just click on the HTML file you want to view.
现在,当您访问http://localhost:8000/ ,应该会看到目录中所有文件的列表。 然后,您只需单击要查看HTML文件。
Just keep in mind that SimpleHTTPServer
and http.server
are only for testing things locally. They only do very basic security checks and shouldn't be used in production.
请记住, SimpleHTTPServer
和http.server
仅用于本地测试。 它们仅执行非常基本的安全检查,不应在生产中使用。
(How to send files locally)
To set up a sort of quick and dirty NAS (Network Attached Storage) system:
要设置一种快速而肮脏的NAS(网络附加存储)系统,请执行以下操作:
- Make sure both computers are connected through same network via LAN or WiFi
- Open your command prompt or terminal and run
python -V
to make sure Python is installed
打开命令提示符或终端并运行python -V
以确保已安装Python - Go to the directory whose file you want to share by using cd (change directory) command.
- Go to the directory with the file you want to share using
cd
on *nix or MacOS systems orCD
for Windows
使用* nix或MacOS系统上的cd
或Windows的CD
转到要共享文件的目录 - Start your HTTP server with either
python -m SimpleHTTPServer
orpython3 -m http.server
使用python -m SimpleHTTPServer
或python3 -m http.server
启动HTTP服务器 - Open new terminal and type
ifconfig
on *nix or MacOS oripconfig
on Windows to find your IP address
打开新终端,然后在* nix或MacOS上键入ifconfig
或在Windows上键入ipconfig
来找到您的IP地址
Now on the second computer or device:
现在在第二台计算机或设备上:
- Open browser and type in the IP address of the first machine, along with port 8000:
http://[ip address]:8000
打开浏览器,输入第一台计算机的IP地址以及端口8000:http://[ip address]:8000
A page will open showing all the files in the directory being shared from the first computer. If the page is taking too long to load, you may need to adjust the firewall settings on the first computer.
将打开一个页面,显示从第一台计算机共享的目录中的所有文件。 如果页面加载时间太长,则可能需要在第一台计算机上调整防火墙设置。
翻译自: https://www.freecodecamp.org/news/simplehttpserver-explained-how-to-send-files-using-python/