Skip to content

Instantly share code, notes, and snippets.

@rtrouton
Created Sep 14, 2021
Embed
What would you like to do?
Script which creates a Python-hosted web server for TCP connection testing
#!/bin/bash
webdirectory=$(mktemp -d)
# Set port number for web service
port_number="8080"
# Create temporary directory and change directory
# into the temporary directory
cd "$webdirectory"
cat > "$webdirectory"/index.html << 'Index'
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>
Index
# Run webservice
/usr/bin/python3 -m http.server "$port_number"
@kovacs-andras

This comment has been minimized.

Copy link

@kovacs-andras kovacs-andras commented Sep 27, 2021

while true; do
  echo -e "HTTP/1.1 200 OK\n\n <html><head><title>Hello World</title></head><body>Hello World</body></html>" \
  | nc -l 8080 
done

Ofc you can pass the "content" many ways like heredoc, from a file, etc...

@kovacs-andras

This comment has been minimized.

Copy link

@kovacs-andras kovacs-andras commented Sep 27, 2021

The webdirectory variable would be useful only if... you would remove the folder at the end of the script or something, otherwise you can cd directly into the temp folder like:
cd $(mktemp -d)
And the port number could be optional like an argument ($1?). Without any argument it would try to bind on 8000/tcp.
But I would still use for a simple task like this netcat instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment