Skip to content

Instantly share code, notes, and snippets.

@cvmiller
Forked from akorobov/ipv6-httpd.py
Last active February 26, 2020 10:24
Show Gist options
  • Save cvmiller/1698c2287a0db800b3a1227dcd976dfd to your computer and use it in GitHub Desktop.
Save cvmiller/1698c2287a0db800b3a1227dcd976dfd to your computer and use it in GitHub Desktop.
quick ipv6 http server using python's SimpleHttpServer
#!/usr/bin/env python3
"""
Python 3 webserver which supports IPv6
original from:
https://gist.github.com/akorobov/7903307
Pseudo path of /ip will report client IP address back to client,
otherwise, shows directory index
Modified to work under python3 by Craig Miller 23 June 2017
Version 0.92
"""
#
# Issue for python 3.4 when ^C is pressed to quit server
# http://bugs.python.org/issue14574
#
# port webserver listens to
listen_port = 8080
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler
import signal
import sys
# setup global for server request loop
shutdown_requested = False
# signal handlder for SIGINT
def sigint_handler(signal, frame):
global shutdown_requested
shutdown_requested = True
print("Caught SIGINT, dying")
sys.exit(0)
# register SIGINT signal handler
signal.signal(signal.SIGINT, sigint_handler)
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
# if path is /ip then print client IP address (v4 or v6)
if self.path == '/ip':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
answer = 'Your IP address is ' + self.client_address[0]
# convert string 'answer' to binary for buffer output
self.wfile.write(str.encode(answer))
return
else:
return SimpleHTTPRequestHandler.do_GET(self)
class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6
def main():
server = HTTPServerV6(('::', listen_port), MyHandler)
print('Press ^C to quit')
while not shutdown_requested:
try:
server.handle_request()
except:
# get out of loop
pass
sys.exit(0)
if __name__ == '__main__':
main()
@cvmiller
Copy link
Author

cvmiller commented Jun 25, 2017

#!/usr/bin/env python3

"""
    Python 3 webserver which supports IPv6
    original from:
        https://gist.github.com/akorobov/7903307
    
    Pseudo path of /ip will report client IP address back to client, 
        otherwise, shows directory index

    Modified to work under python3 by Craig Miller 23 June 2017
    Version 0.9
"""

#
# Issue for python 3.4 when ^C is pressed to quit server 
#   http://bugs.python.org/issue14574
#


# port webserver listens to
listen_port = 8080

import os
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler

class MyHandler(SimpleHTTPRequestHandler):
    def do_GET(self):
        # if path is /ip then print client IP address (v4 or v6)
        if self.path == '/ip':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            answer = 'Your IP address is ' + self.client_address[0]
            # convert string 'answer' to binary for buffer output
            self.wfile.write(str.encode(answer))
            return
        else:
            return SimpleHTTPRequestHandler.do_GET(self)

class HTTPServerV6(HTTPServer):
    address_family = socket.AF_INET6

def main():
    server = HTTPServerV6(('::', listen_port), MyHandler)
    print('Press ^C twice to quit')
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("Detected ^C")
        server.socket.close()
        os._exit(1)


if __name__ == '__main__':
    main()

@cvmiller
Copy link
Author

A version which works under python3

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