python httpserver 支持ipv6

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wojiuguowei/article/details/83823826

在创建HTTPServer实例之前将address_family设置成socket.AF_INET6

#!/usr/bin/python
import commands
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import socket,SocketServer

PORT_NUMBER = 8080

class myHandler(BaseHTTPRequestHandler):

    #Handler for the GET requests

    def do_GET(self):

        print 'get method'

        return

try:
    #Create a web server and define the handler to manage the
    #incoming request

    cmd = "ip addr|grep 'scope global'|grep inet6|awk -F' ' '{print $2}'|awk -F'/' '{print $1}'"
    (status,ipv6) = commands.getstatusoutput(cmd)

    if ipv6 != "" :
        SocketServer.TCPServer.address_family=socket.AF_INET6

    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

猜你喜欢

转载自blog.csdn.net/wojiuguowei/article/details/83823826