LoginSignup
0
0

More than 1 year has passed since last update.

[Shell] Create a simple server with shell script

Posted at

Introduction

Sometimes when our application is not ready for devployment, but we hope to setup a environment to test the network connectivity, host a simple test server is a really usefull. If your server or container contains python, it will be very easy with command like this python -m http.server 8000 to achieve this. but if your environment does not have python. Then it also has a way to create a simple server.

Create a simple server with shell script

On Unix-like operating systems, the nc command runs Netcat, a utility for sending raw data over a network connection. We can use nc coomand to host a http server.

#!/bin/sh

# response
response() {
    echo "HTTP/1.0 200 OK"
    echo "Content-Type: text/plain"
    echo ""
    echo "Hello"
}

# listen ctrl-c
trap exit INT

# loop and listen port 8000
# if you want to listen IPv6, use -l6
while true; do
    response | nc -l 8000 -w 1
done
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0