Skip to content

Instantly share code, notes, and snippets.

@akorobov
Created December 11, 2013 00:58
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save akorobov/7903307 to your computer and use it in GitHub Desktop.
Save akorobov/7903307 to your computer and use it in GitHub Desktop.
quick ipv6 http server using python's SimpleHttpServer
import socket
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/ip':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Your IP address is %s' % self.client_address[0])
return
else:
return SimpleHTTPRequestHandler.do_GET(self)
class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6
def main():
server = HTTPServerV6(('::', 8080), MyHandler)
server.serve_forever()
if __name__ == '__main__':
main()
@rubo77
Copy link

rubo77 commented Nov 24, 2015

Great, this just works!

an output where to connect would be nice though

@ckorn
Copy link

ckorn commented Jul 30, 2016

Perfect for https://github.com/diafygi/letsencrypt-nosudo when serving the challenge over IPv6. Now that it is finally supported: https://letsencrypt.org//2016/07/26/full-ipv6-support.html

@vdloo
Copy link

vdloo commented Sep 10, 2016

Thanks. Also works in python 3 if you replace

from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

with

from http.server import HTTPServer, SimpleHTTPRequestHandler

@li6in9muyou
Copy link

Thanks. The /ip endpoint works in python 3 if you replace

self.wfile.write('Your IP address is %s' % self.client_address[0])

with

self.wfile.write(f'Your IP address is {self.client_address[0]}'.encode())

Fully working example in python 3:

import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler

class MyHandler(SimpleHTTPRequestHandler):
  def do_GET(self):
    if self.path == '/ip':
      self.send_response(200)
      self.send_header('Content-type', 'text/html')
      self.end_headers()
      self.wfile.write(f'Your IP address is {self.client_address[0]}'.encode())
      return
    else:
      return SimpleHTTPRequestHandler.do_GET(self)

class HTTPServerV6(HTTPServer):
  address_family = socket.AF_INET6

def main():
  server = HTTPServerV6(('::', 8080), MyHandler)
  server.serve_forever()

if __name__ == '__main__':
  main()

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