国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

tornado獲得客戶端設備信息和ip地址

2019-11-14 17:29:10
字體:
來源:轉載
供稿:網友

最初看到的關于如何獲取客戶端ip地址的博客是在Tornado獲取客戶端IP

然后 查看tornado的httpserver.py源碼,發現可以直接用self.request.remote_ip獲得客戶端地址:

 

    def __str__(self):        if self.address_family in (socket.AF_INET, socket.AF_INET6):            return self.remote_ip        elif isinstance(self.address, bytes):            # Python 3 with the -bb option warns about str(bytes),            # so convert it explicitly.            # Unix socket addresses are str on mac but bytes on linux.            return native_str(self.address)        else:            return str(self.address)    def _apply_xheaders(self, headers):        """Rewrite the ``remote_ip`` and ``PRotocol`` fields."""        # Squid uses X-Forwarded-For, others use X-Real-Ip        ip = headers.get("X-Forwarded-For", self.remote_ip)        ip = ip.split(',')[-1].strip()        ip = headers.get("X-Real-Ip", ip)        if netutil.is_valid_ip(ip):            self.remote_ip = ip        # AWS uses X-Forwarded-Proto        proto_header = headers.get(            "X-Scheme", headers.get("X-Forwarded-Proto",                                    self.protocol))        if proto_header in ("http", "https"):            self.protocol = proto_header    def _unapply_xheaders(self):        """Undo changes from `_apply_xheaders`.        Xheaders are per-request so they should not leak to the next        request on the same connection.        """        self.remote_ip = self._orig_remote_ip        self.protocol = self._orig_protocol

于是覺得可以這樣子做:

''' Copyright (c) HuangJunJie@SYSU(SNO13331087). All Rights Reserved. '''''' get_user-agent_and_ip.py: catch the headers and ip of the client '''import tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.optionsimport os.pathfrom tornado.options import define, optionsdefine("port", default=8888, help="run on the given port", type=int)class MainHandler(tornado.web.RequestHandler):    def get(self):        self.write(self.request.headers['user-agent'] +/            "/nyour current ip is: "+self.request.remote_ip)if __name__ == "__main__":    application = tornado.web.Application([(r"/", MainHandler)], debug = True)    http_server = tornado.httpserver.HTTPServer(application, xheaders=True)    http_server.listen(options.port)    tornado.ioloop.IOLoop.instance().start()

 在本地的瀏覽器訪問和使用手機該筆記本發射的wifi訪問得到了如下結果:

 但是仍然無法在外網連上服務器。希望有大牛在此留下評論,提供解決方法或思路。


上一篇:安裝pillow

下一篇:安裝python-docx

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达拉特旗| 衡山县| 临湘市| 宝鸡市| 济南市| 嫩江县| 宁武县| 阿拉善右旗| 神池县| 香河县| 赫章县| 遂宁市| 潢川县| 饶河县| 正定县| 兴义市| 唐海县| 绍兴市| 延川县| 正镶白旗| 岗巴县| 南平市| 石门县| 招远市| 乐安县| 双峰县| 文昌市| 全南县| 宁都县| 仙游县| 苏尼特右旗| 永丰县| 宝鸡市| 容城县| 滨州市| 马山县| 商城县| 周宁县| 辽中县| 平山县| 普宁市|