In python, issue a get request to a ipv6 link-loca

2019-12-16 20:28发布

问题:

Is there a standard way to issue an HTTP GET request to a url using any standard python library, with the major caveat that the host address is an ipv6 link-local address (heavy emphasis on "link-local")? The target machine that is running the server does not have any ip address other than the raw link-local ip address that includes the mac address (hence it can only be reached by an address that looks something like "fe80:::%eth0" where "%eth0" is the network interface on the client that shares a broadcast domain with the server).

回答1:

You can do this with Twisted's HTTP client API:

from __future__ import print_function

from sys import argv

from twisted.internet.endpoints import TCP6ClientEndpoint
from twisted.web.client import ProxyAgent
from twisted.internet.task import react

def main(reactor, address, uri):
    server = TCP6ClientEndpoint(reactor, address, 80)
    agent = ProxyAgent(server, reactor)
    getting = agent.request(b"GET", uri)
    def got(response):
        print("Got {}".format(response.code))
    getting.addCallback(got)
    return getting

if __name__ == "__main__":
    react(main, argv[1:])

For example:

$ python http-proxy-get.py ::1 http://example.com/
Got 200
$