Sometimes as admins, we need to share files across systems quickly. For whatever reason, we need to get a single tar or conf file from one system to another, or maybe copy a group of files between systems. We find that we need to install a package or utility to share the files, or we may need to enable a feature to share the content. Regardless of the situation, we can use Python's SimpleHTTPServer module as a quick web server.

Python -m SimpleHTTPServer

The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory.

Usage

Python must be installed to use the SimpleHTTPServer module. Python may be installed as a dependency to an application or service that is already running on the system. Run python -V to verify its presence. To use the module for our purposes, run the following command from the directory with the files we want to share:

python -m SimpleHTTPServer

* Note: The module has been renamed http.server in python3

The module loads and serves any files within the directory on port 8000 by default.

8000

From the remote system, browse to http://<ip>:8000 using your browser of choice. A directory listing will be displayed where you can open or download any file.

dircropped

Another option is to use wget or curl to download the files to the remote system.

wget http://<ip>:8000/filename
curl http://<ip>:8000/filename

This simple local HTTP server is a nice little utility to use in those times when you need to transfer files quickly across systems.

Use cases

This tip is not a replacement for actual file-hosting solutions in business environments. The SimpleHTTPServer module is actually a development tool used as a framework tool to lay the foundation for a full-blown web server with all the necessary security features included. However, we can use the module for our purposes to:

  • Quickly serve kickstart files in a pinch - Copy the /root/anaconda-ks.cfg or /root/original-ks.cfg to a folder and serve with python -m SimpleHTTPServer. Edit the grub config at boot, and away you go.
  • Share a file to/from a VM - Copy a file to a folder and serve with python -m SimpleHTTPServer.
  • Serve a file on an allowed port of firewall - Copy a file to a folder and serve with python -m SimpleHTTPServer port

Other options

You can also use a while loop with ncat or /dev/tcp/host/port for transport… but that is another article. I hope you find this option a useful tool in your toolbox.

Gotchas

You may need to open ports in your firewall:

# firewall-cmd --add-port=portnumber/tcp

[ Download now: A sysadmin's guide to Bash scripting. ]


关于作者

Stephen Wilson is a Senior Storage Consultant with Red Hat, Inc. He has over 20 years of experience in information systems management. His professional interests include system administration, cybersecurity, cloud technologies, and virtualization.


Stephen lives in Meridian, MS with his wife Tan and two boys, Stephen and Matthew. Stephen's personal hobbies include weightlifting, running (yes for fun), and basketball. Stephen is active in his community and volunteers his time to try and make things better for everybody

UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来