{"id":696,"date":"2014-07-22T15:41:00","date_gmt":"2014-07-22T15:41:00","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=696"},"modified":"2014-07-22T15:41:00","modified_gmt":"2014-07-22T15:41:00","slug":"%e7%94%a8python%e7%bb%9f%e8%ae%a1nginx%e6%97%a5%e5%bf%97%e4%b8%adip%e8%ae%bf%e9%97%ae%e6%ac%a1%e6%95%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/696.html","title":{"rendered":"\u7528Python\u7edf\u8ba1nginx\u65e5\u5fd7\u4e2dIP\u8bbf\u95ee\u6b21\u6570"},"content":{"rendered":"<p>\u5728\u901b\u5927\u725b\u7684blog\u65f6\u53d1\u73b0\u7684\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u8f6c\u8fc7\u6765\u4e86`(*\u2229_\u2229*)\u2032\uff0c\u6211\u81ea\u5df1\u4e00\u822c\u662f\u7528awk\u8fdb\u884c\u7edf\u8ba1\u7684\uff08\u5e76\u4e14\u7528\u5230\u4e86\u4e4b\u524d\u641c\u96c6\u5230\u7684\u4e00\u4e2a\u7eaf\u771fIP\u5730\u5740\u5e93\u7684Python\u89e3\u6790\u4ee3\u7801\u8fdb\u884c\u4e86\u5730\u7406\u4f4d\u7f6e\u7684\u7edf\u8ba1\uff0c\u5927\u6982\u5c31\u662f\uff1a\u8bbf\u95ee\u6b21\u6570\u3001IP\u5730\u5740\u3001\u5730\u7406\u4f4d\u7f6e\u4fe1\u606f\uff0c\u5982\uff1a\u201c<span style=\"color: #000000;\">578 111.206.61.180 \u5317\u4eac\u5e02\/\u8054\u901a\u201d<\/span>\uff09\uff0c\u8fd9\u91cc\u7528\u7684\u662fPython\uff0c\u5b66\u5230\u4e86\u5982\u4f55\u7528Python\u8fdb\u884c\u53bb\u91cd\uff08\u601d\u60f3\u5c31\u662f\uff1a\u5c06IP\u5730\u5740\u5b58\u4e3a\u6570\u7ec4\u4e0b\u6807\u8fdb\u884c\u53bb\u91cd\u4ee5\u53ca\u8ba1\u6570\uff0c\u7136\u540e\u5c06IP\u5730\u5740\u548c\u8bbf\u95ee\u6b21\u6570\u4f5c\u4e3a\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\uff09\u4ee5\u53ca\u6392\u5e8f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\n\nimport sys\n\nclass Log:\n    def __init__(self, filename, dic, count):\n        self.filename = filename\n        self.dic = dic\n        self.count = count\n\n    def parse(self):\n        i = 1\n        f = file(self.filename)\n        while True:\n            line = f.readline()\n            if len(line)==0:\n                break\n            ip = line.split(' ')\n            if ip[0] in dic:\n                self.dic[ip[0]] = self.dic[ip[0]]+1\n            else:\n                self.dic[ip[0]] = i\n        soredic = sorted(self.dic.items(), key=lambda d:d[1], reverse=True)\n        counts = 0;\n        for item in soredic:\n            if counts==int(self.count):\n                break\n            print(\"IP: %stTotal Times: %s\"%(item[0], item[1]))\n            counts = counts+1\n        f.close()\n\nif __name__==\"__main__\":\n    if len(sys.argv)&lt;3:\n        print('ntUsage: python parseLog.py \/path\/to\/access.log topXntExample: python parseLog.py \/path\/to\/access.log 20')\n        sys.exit()\n    dic={}\n    log=Log(sys.argv[1], dic, sys.argv[2])\n    log.parse()<\/pre>\n<p>\u6548\u679c\u4e5f\u4e0d\u9519\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u548c\u4e4b\u524d\u7684\u89e3\u6790IP\u5730\u5740\u7684Python\u4ee3\u7801\u8fdb\u884c\u6574\u5408\u4e86\u3002<\/p>\n<p>\u7136\u540e\u60f3\u591a\u8bf4\u7684\u4e00\u53e5\u5c31\u662f\uff0c\u4e4b\u524d\u4ecb\u7ecd\u8fc7\u7684ngxtop\u90a3\u4e2a\u5de5\u5177\u5c31\u662f\u7528Python\u5199\u7684\uff0c\u529f\u80fd\u548c\u6548\u679c\u6bd4\u8f83\u7ed9\u529b\uff0c\u5927\u5bb6\u53ef\u4ee5\u8bd5\u8bd5\uff1a<a title=\"ngxtop\uff1a\u5728\u547d\u4ee4\u884c\u5b9e\u65f6\u76d1\u63a7 Nginx \u7684\u795e\u5668\" href=\"http:\/\/ixyzero.com\/blog\/archives\/162.html\" target=\"_blank\">ngxtop\uff1a\u5728\u547d\u4ee4\u884c\u5b9e\u65f6\u76d1\u63a7 Nginx \u7684\u795e\u5668<\/a>\u3002<\/p>\n<p>&nbsp;<\/p>\n<h6>\u539f\u6587\u5730\u5740\uff1a<\/h6>\n<p><a href=\"http:\/\/www.iswin.org\/a\/14\" target=\"_blank\">http:\/\/www.iswin.org\/a\/14<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u901b\u5927\u725b\u7684blog\u65f6\u53d1\u73b0\u7684\u4ee3\u7801\uff0c\u7136\u540e\u5c31\u8f6c\u8fc7\u6765\u4e86`(*\u2229_\u2229*)\u2032\uff0c\u6211\u81ea\u5df1\u4e00\u822c\u662f\u7528awk\u8fdb\u884c\u7edf\u8ba1\u7684\uff08\u5e76\u4e14\u7528\u5230\u4e86\u4e4b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[8,258],"class_list":["post-696","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-python","tag-sorted"],"views":5096,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/696\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}