{"id":96,"date":"2014-06-24T15:55:59","date_gmt":"2014-06-24T15:55:59","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=96"},"modified":"2024-04-28T17:30:41","modified_gmt":"2024-04-28T09:30:41","slug":"nginx%e8%84%9a%e6%9c%ac%e5%88%87%e5%89%b2%e4%bb%a5%e5%8f%8aip%e5%9c%b0%e5%9d%80%e6%89%b9%e9%87%8f%e6%9f%a5%e8%af%a2%e7%9a%84%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/96.html","title":{"rendered":"Nginx\u811a\u672c\u5207\u5272\u4ee5\u53caIP\u5730\u5740\u6279\u91cf\u67e5\u8be2\u7684\u811a\u672c"},"content":{"rendered":"<p>\u4e5f\u662f\u4e4b\u524d\u7528LNMP\u4e00\u952e\u5b89\u88c5\u5305\u7684\u65f6\u5019\u91cc\u9762\u5305\u542b\u7684\u4e00\u4e2anginx\u65e5\u5fd7\u5207\u5272\u811a\u672c\u770b\u4e86\u4e00\u4e0b\u89c9\u5f97\u5f88\u6709\u7528\uff0c\u4e4b\u524d\u4e5f\u4e00\u76f4\u5728\u7528\uff0c\u4f46\u4eca\u5929\u7a81\u7136\u60f3\u5230\u8fd9\u6837\u53ea\u662f\u5207\u5272\uff0c\u4f46\u6ca1\u6709\u5bf9\u65e5\u5fd7\u8fdb\u884c\u5904\u7406\u7684\u8bdd\u6709\u70b9\u6d6a\u8d39\u4e86\uff08\u65e5\u5fd7\u5207\u5272\u4e00\u65b9\u9762\u662f\u4e3a\u4e86\u901f\u5ea6\u8003\u8651\uff0c\u56e0\u4e3a\u65e5\u5fd7\u6587\u4ef6\u4f1a\u6162\u6162\u589e\u5927\u964d\u4f4e\u5904\u7406\u6548\u7387\uff1b\u53e6\u4e00\u65b9\u9762\u662f\u4e3a\u4e86\u4f7f\u7528\u522b\u7684\u5de5\u5177\u6765\u5206\u6790\u65e5\u5fd7\u4fbf\u4e8e\u505a\u51fa\u76f8\u5e94\u5904\u7406\uff09\uff0c\u7ed3\u5408\u4e4b\u524d\u6536\u96c6\u5230\u7684\u7eaf\u771fIP\u6570\u636e\u5e93\u5904\u7406\u7684Python\u811a\u672c(\u56e0\u4e3a\u901f\u5ea6\u5feb\u6240\u4ee5\u4f7f\u7528)\uff0c\u81ea\u5df1\u6539\u5199\u4e86\u4e00\u4e0b\u4e4b\u540e\u53d1\u73b0\u6548\u679c\u8fd8\u4e0d\u9519\uff0c\u53ef\u4ee5\u8fdb\u884c\u6279\u91cf\u67e5\u8be2\uff0c\u7eaf\u771fIP\u7684\u51c6\u786e\u5ea6\u4e5f\u4e0d\u9519\uff0c\u6240\u4ee5\u4eca\u665a\u82b1\u4e86\u51e0\u4e2a\u5c0f\u65f6\u7684\u65f6\u95f4\u6765\u5f04\uff08\u539f\u60f3\u77401\u4e2a\u5c0f\u65f6\u5dee\u4e0d\u591a\u5c31\u53ef\u4ee5\u641e\u5b9a\u7684\uff0c\u540e\u6765\u641e\u4e86\u51e0\u4e2a\u5c0f\u65f6\u624d\u5f04\u597d\uff0c\u5509\uff0c\u5bf9\u65f6\u95f4\u7684\u611f\u89c9\u4e5f\u592a\u5dee\u4e86\u2026\u2026\uff09<\/p>\n<h6>1.\u7528Python\u5199\u7684IP\u5730\u5740\u6279\u91cf\u67e5\u8be2\u811a\u672c\uff1a<\/h6>\n<pre class=\"lang:python decode:true\">#!\/usr\/bin\/env python\n#coding: utf-8\n\n'''\u7528Python\u811a\u672c\u67e5\u8be2\u7eaf\u771fIP\u5e93\nQQWry.Dat\u7684\u683c\u5f0f\u5982\u4e0b:\n+----------+\n|  \u6587\u4ef6\u5934  |  (8\u5b57\u8282)\n+----------+\n|  \u8bb0\u5f55\u533a  | \uff08\u4e0d\u5b9a\u957f\uff09\n+----------+\n|  \u7d22\u5f15\u533a  | \uff08\u5927\u5c0f\u7531\u6587\u4ef6\u5934\u51b3\u5b9a\uff09\n+----------+\n\u6587\u4ef6\u5934\uff1a4\u5b57\u8282\u5f00\u59cb\u7d22\u5f15\u504f\u79fb\u503c+4\u5b57\u8282\u7ed3\u5c3e\u7d22\u5f15\u504f\u79fb\u503c\n\u8bb0\u5f55\u533a\uff1a \u6bcf\u6761IP\u8bb0\u5f55\u683c\u5f0f ==&gt; IP\u5730\u5740[\u56fd\u5bb6\u4fe1\u606f][\u5730\u533a\u4fe1\u606f]\n   \u5bf9\u4e8e\u56fd\u5bb6\u8bb0\u5f55\uff0c\u53ef\u4ee5\u6709\u4e09\u79cd\u8868\u793a\u65b9\u5f0f\uff1a\n       \u5b57\u7b26\u4e32\u5f62\u5f0f(IP\u8bb0\u5f55\u7b2c5\u5b57\u8282\u4e0d\u7b49\u4e8e0x01\u548c0x02\u7684\u60c5\u51b5)\uff0c\n       \u91cd\u5b9a\u5411\u6a21\u5f0f1(\u7b2c5\u5b57\u8282\u4e3a0x01),\u5219\u63a5\u4e0b\u67653\u5b57\u8282\u4e3a\u56fd\u5bb6\u4fe1\u606f\u5b58\u50a8\u5730\u7684\u504f\u79fb\u503c\n       \u91cd\u5b9a\u5411\u6a21\u5f0f(\u7b2c5\u5b57\u8282\u4e3a0x02),\n   \u5bf9\u4e8e\u5730\u533a\u8bb0\u5f55\uff0c\u53ef\u4ee5\u6709\u4e24\u79cd\u8868\u793a\u65b9\u5f0f\uff1a \u5b57\u7b26\u4e32\u5f62\u5f0f\u548c\u91cd\u5b9a\u5411\n   \u6700\u540e\u4e00\u6761\u89c4\u5219\uff1a\u91cd\u5b9a\u5411\u6a21\u5f0f1\u7684\u56fd\u5bb6\u8bb0\u5f55\u540e\u4e0d\u80fd\u8ddf\u5730\u533a\u8bb0\u5f55\n\u7d22\u5f15\u533a\uff1a \u6bcf\u6761\u7d22\u5f15\u8bb0\u5f55\u683c\u5f0f ==&gt; 4\u5b57\u8282\u8d77\u59cbIP\u5730\u5740 + 3\u5b57\u8282\u6307\u5411IP\u8bb0\u5f55\u7684\u504f\u79fb\u503c\n   \u7d22\u5f15\u533a\u7684IP\u548c\u5b83\u6307\u5411\u7684\u8bb0\u5f55\u533a\u4e00\u6761\u8bb0\u5f55\u4e2d\u7684IP\u6784\u6210\u4e00\u4e2aIP\u8303\u56f4\u3002\u67e5\u8be2\u4fe1\u606f\u662f\u8fd9\u4e2a\n   \u8303\u56f4\u5185IP\u7684\u4fe1\u606f\n'''\n\nimport sys\nimport socket\nfrom struct import pack, unpack\n\nclass IPInfo(object):\n    '''QQWry.Dat\u6570\u636e\u5e93\u67e5\u8be2\u529f\u80fd\u96c6\u5408\n    '''\n    def __init__(self, dbname):\n        ''' \u521d\u59cb\u5316\u7c7b\uff0c\u8bfb\u53d6\u6570\u636e\u5e93\u5185\u5bb9\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\n        \u901a\u8fc7\u5f00\u59cb8\u5b57\u8282\u786e\u5b9a\u6570\u636e\u5e93\u7684\u7d22\u5f15\u4fe1\u606f'''\n        self.dbname = dbname\n        # f = file('\/root\/QQwry.dat', 'r')  #\u8fd9\u91cc\u9700\u8981\u76f4\u63a5\u6307\u5b9a\u6570\u636e\u5e93\u7684\u8def\u5f84\uff01\n        # Demon\u6ce8\uff1a\u5728Windows\u4e0b\u7528'r'\u4f1a\u6709\u95ee\u9898\uff0c\u4f1a\u628arn\u8f6c\u6362\u6210n\n        # \u8be6\u89c1http:\/\/demon.tw\/programming\/python-open-mode.html\n        # \u8fd8\u6709Python\u6587\u6863\u4e2d\u4e0d\u63d0\u5021\u7528file\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u63a8\u8350\u7528open\n        f = open(dbname, 'rb')\n        self.img = f.read()\n        f.close()\n        # QQWry.Dat\u6587\u4ef6\u7684\u5f00\u59cb8\u5b57\u8282\u662f\u7d22\u5f15\u4fe1\u606f,\u524d4\u5b57\u8282\u662f\u5f00\u59cb\u7d22\u5f15\u7684\u504f\u79fb\u503c\uff0c\n        # \u540e4\u5b57\u8282\u662f\u7ed3\u675f\u7d22\u5f15\u7684\u504f\u79fb\u503c\u3002\n        # (self.firstIndex, self.lastIndex) = unpack('II', self.img[:8])\n        # Demon\u6ce8\uff1aunpack\u9ed8\u8ba4\u4f7f\u7528\u7684endian\u662f\u548c\u673a\u5668\u6709\u5173\u7684\n        # Intel x86\u548cAMD64(x86-64)\u662flittle-endian\n        # Motorola 68000\u548cPowerPC G5\u662fbig-endian\n        # \u800c\u7eaf\u771f\u6570\u636e\u5e93\u5168\u90e8\u91c7\u7528\u4e86little-endian\u5b57\u8282\u5e8f\n        # \u6240\u4ee5\u5728\u67d0\u4e9bbig-endian\u7684\u673a\u5668\u4e0a\u539f\u4ee3\u7801\u4f1a\u51fa\u9519\n        (self.firstIndex, self.lastIndex) = unpack('&lt;II', self.img[:8])\n        # \u6bcf\u6761\u7d22\u5f15\u957f7\u5b57\u8282\uff0c\u8fd9\u91cc\u5f97\u5230\u7d22\u5f15\u603b\u4e2a\u6570\n        self.indexCount = (self.lastIndex - self.firstIndex) \/ 7 + 1\n    def getString(self, offset = 0):\n        ''' \u8bfb\u53d6\u5b57\u7b26\u4e32\u4fe1\u606f\uff0c\u5305\u62ec\"\u56fd\u5bb6\"\u4fe1\u606f\u548c\"\u5730\u533a\"\u4fe1\u606f\n        QQWry.Dat\u7684\u8bb0\u5f55\u533a\u6bcf\u6761\u4fe1\u606f\u90fd\u662f\u4e00\u4e2a\u4ee5'\u0000'\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32'''\n        o2 = self.img.find('\u0000', offset)\n        #return self.img[offset:o2]\n        # \u6709\u53ef\u80fd\u53ea\u6709\u56fd\u5bb6\u4fe1\u606f\u6ca1\u6709\u5730\u533a\u4fe1\u606f\uff0c\n        gb2312_str = self.img[offset:o2]\n        try:\n            utf8_str = unicode(gb2312_str,'gb2312').encode('utf-8')\n        except:\n            return '\u672a\u77e5'\n        return utf8_str\n    def getLong3(self, offset = 0):\n        '''QQWry.Dat\u4e2d\u7684\u504f\u79fb\u8bb0\u5f55\u90fd\u662f3\u5b57\u8282\uff0c\u672c\u51fd\u6570\u53d6\u5f973\u5b57\u8282\u7684\u504f\u79fb\u91cf\u7684\u5e38\u89c4\u8868\u793a\n        QQWry.Dat\u4f7f\u7528\u201c\u5b57\u7b26\u4e32\u201c\u5b58\u50a8\u8fd9\u4e9b\u503c'''\n        s = self.img[offset: offset + 3]\n        s += '\u0000'\n        # unpack\u7528\u4e00\u4e2a'I'\u4f5c\u4e3aformat\uff0c\u540e\u9762\u7684\u5b57\u7b26\u4e32\u5fc5\u987b\u662f4\u5b57\u8282\n        # return unpack('I', s)[0]\n        # Demon\u6ce8\uff1a\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u5f3a\u5236\u4f7f\u7528little-endian\n        return unpack('&lt;I', s)[0]\n    def getAreaAddr(self, offset = 0):\n        ''' \u901a\u8fc7\u7ed9\u51fa\u504f\u79fb\u503c\uff0c\u53d6\u5f97\u533a\u57df\u4fe1\u606f\u5b57\u7b26\u4e32\uff0c'''\n        byte = ord(self.img[offset])\n        if byte == 1 or byte == 2:\n            # \u7b2c\u4e00\u4e2a\u5b57\u8282\u4e3a1\u6216\u80052\u65f6\uff0c\u53d6\u5f972-4\u5b57\u8282\u4f5c\u4e3a\u4e00\u4e2a\u504f\u79fb\u91cf\u8c03\u7528\u81ea\u5df1\n            p = self.getLong3(offset + 1)\n            return self.getAreaAddr(p)\n        else:\n            return self.getString(offset)\n    def getAddr(self, offset, ip = 0):\n        img = self.img\n        o = offset\n        byte = ord(img[o])\n        if byte == 1:\n            # \u91cd\u5b9a\u5411\u6a21\u5f0f1\n            # [IP][0x01][\u56fd\u5bb6\u548c\u5730\u533a\u4fe1\u606f\u7684\u7edd\u5bf9\u504f\u79fb\u5730\u5740]\n            # \u4f7f\u7528\u63a5\u4e0b\u6765\u76843\u5b57\u8282\u4f5c\u4e3a\u504f\u79fb\u91cf\u8c03\u7528\u5b57\u8282\u53d6\u5f97\u4fe1\u606f\n            return self.getAddr(self.getLong3(o + 1))\n        if byte == 2:\n            # \u91cd\u5b9a\u5411\u6a21\u5f0f2\n            # [IP][0x02][\u56fd\u5bb6\u4fe1\u606f\u7684\u7edd\u5bf9\u504f\u79fb][\u5730\u533a\u4fe1\u606f\u5b57\u7b26\u4e32]\n            # \u4f7f\u7528\u56fd\u5bb6\u4fe1\u606f\u504f\u79fb\u91cf\u8c03\u7528\u81ea\u5df1\u53d6\u5f97\u5b57\u7b26\u4e32\u4fe1\u606f\n            cArea = self.getAreaAddr(self.getLong3(o + 1))\n            o += 4\n            # \u8df3\u8fc7\u524d4\u5b57\u8282\u53d6\u5b57\u7b26\u4e32\u4f5c\u4e3a\u5730\u533a\u4fe1\u606f\n            aArea = self.getAreaAddr(o)\n            return (cArea, aArea)\n        if byte != 1 and byte != 2:\n            # \u6700\u7b80\u5355\u7684IP\u8bb0\u5f55\u5f62\u5f0f\uff0c[IP][\u56fd\u5bb6\u4fe1\u606f][\u5730\u533a\u4fe1\u606f]\n            # \u91cd\u5b9a\u5411\u6a21\u5f0f1\u6709\u79cd\u60c5\u51b5\u5c31\u662f\u504f\u79fb\u91cf\u6307\u5411\u5305\u542b\u56fd\u5bb6\u548c\u5730\u533a\u4fe1\u606f\u4e24\u4e2a\u5b57\u7b26\u4e32\n            # \u5373\u504f\u79fb\u91cf\u6307\u5411\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u4e0d\u662f1\u62162,\u5c31\u4f7f\u7528\u8fd9\u91cc\u7684\u5206\u652f\n            # \u7b80\u5355\u5730\u8bf4\uff1a\u53d6\u8fde\u7eed\u53d6\u4e24\u4e2a\u5b57\u7b26\u4e32\uff01\n            cArea = self.getString(o)\n            #o += len(cArea) + 1\n            # \u6211\u4eec\u5df2\u7ecf\u4fee\u6539cArea\u4e3autf-8\u5b57\u7b26\u7f16\u7801\u4e86\uff0clen\u53d6\u5f97\u7684\u957f\u5ea6\u4f1a\u6709\u53d8\uff0c\n            # \u7528\u4e0b\u9762\u65b9\u6cd5\u5f97\u5230offset\n            o = self.img.find('\u0000',o) + 1\n            aArea = self.getString(o)\n            return (cArea, aArea)\n    def find(self, ip, l, r):\n        ''' \u4f7f\u7528\u4e8c\u5206\u6cd5\u67e5\u627e\u7f51\u7edc\u5b57\u8282\u7f16\u7801\u7684IP\u5730\u5740\u7684\u7d22\u5f15\u8bb0\u5f55'''\n        if r - l &lt;= 1:\n            return l\n        m = (l + r) \/ 2\n        o = self.firstIndex + m * 7\n        #new_ip = unpack('I', self.img[o: o+4])[0]\n        # Demon\u6ce8\uff1a\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u5f3a\u5236\u4f7f\u7528little-endian\n        new_ip = unpack('&lt;I', self.img[o: o+4])[0]\n        if ip &lt;= new_ip:\n            return self.find(ip, l, m)\n        else:\n            return self.find(ip, m, r)\n    def getIPAddr(self, ip):\n        ''' \u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u53d6\u5f97\u4fe1\u606f\uff01'''\n        # \u4f7f\u7528\u7f51\u7edc\u5b57\u8282\u7f16\u7801IP\u5730\u5740\n        ip = unpack('!I', socket.inet_aton(ip))[0]\n        # \u4f7f\u7528 self.find \u51fd\u6570\u67e5\u627eip\u7684\u7d22\u5f15\u504f\u79fb\n        i = self.find(ip, 0, self.indexCount - 1)\n        # \u5f97\u5230\u7d22\u5f15\u8bb0\u5f55\n        o = self.firstIndex + i * 7\n        # \u7d22\u5f15\u8bb0\u5f55\u683c\u5f0f\u662f\uff1a \u524d4\u5b57\u8282IP\u4fe1\u606f+3\u5b57\u8282\u6307\u5411IP\u8bb0\u5f55\u4fe1\u606f\u7684\u504f\u79fb\u91cf\n        # \u8fd9\u91cc\u5c31\u662f\u4f7f\u7528\u540e3\u5b57\u8282\u4f5c\u4e3a\u504f\u79fb\u91cf\u5f97\u5230\u5176\u5e38\u89c4\u8868\u793a\uff08QQWry.Dat\u7528\u5b57\u7b26\u4e32\u8868\u793a\u503c\uff09\n        o2 = self.getLong3(o + 4)\n        # IP\u8bb0\u5f55\u504f\u79fb\u503c+4\u53ef\u4ee5\u4e22\u5f03\u524d4\u5b57\u8282\u7684IP\u5730\u5740\u4fe1\u606f\u3002\n        (c, a) = self.getAddr(o2 + 4)\n        return (c, a)\n    def output(self, first, last):\n        for i in range(first, last):\n            o = self.firstIndex +  i * 7\n            ip = socket.inet_ntoa(pack('!I', unpack('I', self.img[o:o+4])[0]))\n            offset = self.getLong3(o + 4)\n            (c, a) = self.getAddr(offset + 4)\n            print \"%s %d %s\/%s\" % (ip, offset, c, a)\n\ndef main():\n\ti = IPInfo('QQWry.dat')\n\tfp = open(\"\/root\/tmp\/ip.txt\", \"r\")\n\tfile_lines = fp.readlines()\n\tfor each_line in  file_lines:\n\t\t(c, a) = i.getIPAddr(each_line)\n\t\t# Demon\u6ce8\uff1a\u5982\u679c\u662f\u5728Windows\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u628a\u7f16\u7801\u8f6c\u56degb2312\u4ee5\u907f\u514d\u4e71\u7801\n\t\tif sys.platform == 'win32':\n\t\t\tc = unicode(c, 'utf-8').encode('gb2312')\n\t\t\ta = unicode(a, 'utf-8').encode('gb2312')\n\t\tprint '%s %s\/%s' % (each_line, c, a)\n\tfp.close()\nif __name__ == '__main__':\n    main()<\/pre>\n<h6>2.\u65e5\u5fd7\u5207\u5272\u811a\u672c+IP\u5730\u5740\u5904\u7406\u529f\u80fd\uff1a<\/h6>\n<pre class=\"lang:sh decode:true\">#!\/bin\/bash\n\n#set the path to nginx log files\nlog_files_path=\"\/home\/wwwlogs\/\"\nlog_files_dir=${log_files_path}$(date -d \"yesterday\" +\"%Y\")\/$(date -d \"yesterday\" +\"%m\")\n#set nginx log files you want to cut\nlog_files_name=(access)\n#set the path to nginx.\nnginx_sbin=\"\/usr\/local\/nginx\/sbin\/nginx\"\n#Set how long you want to save\nsave_days=30\n\n############################################\n#Please do not modify the following script #\n############################################\nmkdir -p $log_files_dir\n\nlog_files_num=${#log_files_name[@]}\nlog_date=$(date -d \"yesterday\" +\"%Y%m%d\")\n\n#cut nginx log files\nfor((i=0;i&lt;$log_files_num;i++));do\nmv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}\/${log_files_name[i]}_$log_date.log\ncp ${log_files_dir}\/${log_files_name[i]}_$log_date.log \/tmp\/access.log\ncd \/tmp\nawk '{dict[$1]++}; END{for(item in dict) print dict[item]\"t\", item}' \/tmp\/access.log | sort -nr &gt;\/tmp\/ip_with_counts.txt\nawk '{print $2}' \/tmp\/ip_with_counts.txt &gt;\/tmp\/ip.txt\npython \/root\/tools\/py_QQwryIP_location.py &gt;\/tmp\/ip2location.txt\npaste \/tmp\/ip_with_counts.txt \/tmp\/ip2location.txt &gt;\/home\/wwwroot\/default\/blog\/$log_date.html\nrm access.log ip*.txt\ndone\n\n#delete 30 days ago nginx log files\nfind $log_files_path -mtime +$save_days -exec rm -rf {} ;\n\n$nginx_sbin -s reload<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<h5>\u671f\u95f4\u9047\u5230\u7684\u95ee\u9898\u53ca\u89e3\u51b3\u529e\u6cd5\uff1a<\/h5>\n<h6>1.Python\u811a\u672c\u65e0\u6cd5\u6b63\u5e38\u8fd0\u884c<\/h6>\n<p>\u5728Windows\u4e0a\u4f7f\u7528\u5f97\u5f88\u597d\u7684Python\u811a\u672c\u5230\u4e86Linux\u73af\u5883\u4e0b\u5c31\u5f00\u59cb\u62a5\u9519\uff1aIOError: [Errno 2] No such file or directory: &#8216;QQWry.dat&#8217;<\/p>\n<p>\u8fd9\u4e2a\u9519\u8bef\u8ba9\u4eba\u5f88\u65e0\u5948\uff0c\u56e0\u4e3aQQwry.dat\u6587\u4ef6\u548cPython\u811a\u672c\u5904\u4e8e\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u5f53\u65f6\u4e5f\u662f\u6ca1\u529e\u6cd5\uff0c\u540e\u6765\u53ea\u6709\u4e00\u4e2a\u4e00\u4e2a\u7684\u53bb\u8bd5\uff0c\u4ecePython\u7684\u5224\u65ad\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1aos.path.exists(&#8216;\/path\/to\/filename&#8217;);\u6d4b\u8bd5\u6ca1\u95ee\u9898\uff0c\u7136\u540e\u6839\u636e\u9519\u8bef\u884c\u6570\u5c31\u4fee\u6539\u5176\u4e2d\u7684\u4ee3\u7801\uff0c\u5c06\u5176\u4e2d\u7684dbname\u76f4\u63a5\u4fee\u6539\u4e3aQQwry.dat\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\uff08\u57fa\u4e8e\u540e\u9762\u7684crontab\u5468\u671f\u6027\u8fd0\u884c\u7684\u8003\u8651\uff0c\u6700\u597d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\uff09\u5c31ok\u4e86\uff1b<\/p>\n<h6>2.\u600e\u4e48\u5904\u7406access.log\u6587\u4ef6\uff1f<\/h6>\n<p>\u540e\u6765\u60f3\u5230\u7684\u662f\u5904\u7406\u5207\u5272\u4e4b\u540e\u7684\u811a\u672c\uff0c\u5148\u590d\u5236\u5230\/tmp\u6587\u4ef6\u5939\u4e0b\uff0c\u7136\u540e\u7528awk\u5bf9\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff0c\u5bf9\u8bbf\u95ee\u7684IP\u5730\u5740\u7684\u6b21\u6570\u8fdb\u884c\u6392\u5e8f\uff08\u7528\u5230\u4e86\u4e4b\u524d\u7684awk\u53bb\u91cd\u7684\u65b9\u6cd5\uff09\uff0c\u7136\u540e\u5982\u4f55\u5c06\u5f97\u5230\u7684\u6587\u4ef6\u8fdb\u884c\u5408\u5e76\uff0c\u56e0\u4e3a\u5bf9awk\u7528\u7684\u4e0d\u719f\uff0c\u4e00\u65f6\u4e4b\u95f4\u8fd8\u4e0d\u77e5\u9053\u8be5\u600e\u4e48\u529e\uff0c\u540e\u6765\u624d\u60f3\u5230\u4e86paste\u547d\u4ee4\uff0c\u518d\u4fee\u6539\u4e00\u4e0bPython\u811a\u672c\u7684\u8f93\u51fa\uff0c\u5c31\u53ef\u4ee5\u4e86\uff0c\u7136\u540e\u81ea\u52a8\u5bfc\u5165\u5230\u7f51\u7ad9\u76ee\u5f55\u4e0b\uff0c\u65b9\u4fbf\u901a\u8fc7Web\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e5f\u662f\u4e4b\u524d\u7528LNMP\u4e00\u952e\u5b89\u88c5\u5305\u7684\u65f6\u5019\u91cc\u9762\u5305\u542b\u7684\u4e00\u4e2anginx\u65e5\u5fd7\u5207\u5272\u811a\u672c\u770b\u4e86\u4e00\u4e0b\u89c9\u5f97\u5f88\u6709\u7528\uff0c\u4e4b\u524d\u4e5f\u4e00\u76f4\u5728\u7528\uff0c\u4f46\u4eca [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12],"tags":[74,75,8,65],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-linux","category-tools","tag-awk","tag-nginx","tag-python","tag-shell"],"views":1852,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/96","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=96"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":5683,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/96\/revisions\/5683"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}