Skip to content

Instantly share code, notes, and snippets.

@rtrouton
Created September 14, 2021 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtrouton/3cbfcc4000e55c5f7b1f71869b3be6b0 to your computer and use it in GitHub Desktop.
Save rtrouton/3cbfcc4000e55c5f7b1f71869b3be6b0 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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