{"id":821,"date":"2009-11-15T23:41:50","date_gmt":"2009-11-15T15:41:50","guid":{"rendered":"http:\/\/www.impjq.net\/blog\/2009\/11\/15\/python-%e5%88%86%e6%9e%90apache-log%e8%84%9a%e6%9c%ac\/"},"modified":"2009-11-15T23:41:50","modified_gmt":"2009-11-15T15:41:50","slug":"python-%e5%88%86%e6%9e%90apache-log%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/pjq.me\/?p=821","title":{"rendered":"Python \u5206\u6790apache log\u811a\u672c"},"content":{"rendered":"<p>\u4e3a\u4e86\u7edf\u8ba1\u6bcf\u4e2aIP\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u507f\u8bd5\u7740\u5199\u4e86\u4e00\u4e2apython\u811a\u672c\uff0c\u5f97\u5230\u524d10\u4e2a\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u7684IP\uff1a<\/p>\n<pre lang=\"python\" line=\"1\">\ndef countstatics(result):\n    \"\"\"Compute the statics.\"\"\"\n    li = result.split('\\n')\n    l = {}\n    for a in set(li):\n        l[a] = 0\n\n    for a in li:\n        l[a] = l[a] + 1\n\n    print \"The uniq ip number is \", len(l)\n    return l\n\ndef apachelog(path):\n    \"\"\"analyse the apachelog,get the top 10 ip address which visit my server\n    \"\"\"\n    print 'analyse the apachelog,get the top 10 ip address which visit my server'\n    cmd = 'cut -d \" \" -f1 ' + path\n    result = exec_shell(cmd)\n    li = countstatics(result)\n\n    l = [\"%s %s\" % (k, v) for v, k in li.items() if k &gt; 1000]\n    l.sort(cmp=None, key=None, reverse=True)\n    #print l\n    for i in range(10):\n        print l[i]\n\n\nif __name__ == \"__main__\":\n    print 'main'\n\n    apachelog(\"\/var\/log\/apache2\/access_log\")\n\n    print 'finished'\n<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\nmain<br \/>\napachelog<br \/>\nThe uniq ip number is  30907<br \/>\n97374 192.168.0.160<br \/>\n8676 59.42.196.130<br \/>\n8313 121.228.230.114<br \/>\n7909 59.49.232.157<br \/>\n7909 221.239.137.161<br \/>\n6130 117.80.191.13<br \/>\n6056 121.227.155.37<br \/>\n5666 119.32.45.219<br \/>\n5633 220.166.172.5<br \/>\n5295 123.124.228.6<br \/>\nfinished<\/p>\n<p>\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u7684\u8fd8\u662f\u672c\u5730IP\uff0c\u518d\u63a5\u4e0awhois\u5c31\u53ef\u4ee5\u67e5\u770b\u8be6\u7ec6\u7684\u4fe1\u606f\u4e86\u3002<\/p>\n<div class=\"zemanta-pixie\"><img decoding=\"async\" class=\"zemanta-pixie-img\" alt=\"\" src=\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\" \/><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u7edf\u8ba1\u6bcf\u4e2aIP\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u507f\u8bd5\u7740\u5199\u4e86\u4e00\u4e2apython\u811a\u672c\uff0c\u5f97\u5230\u524d10\u4e2a\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u7684IP\uff1a def countstatics(result): &#8220;&#8221;&#8221;Compute the statics.&#8221;&#8221;&#8221; li = result.split(&#8216;\\n&#8217;) l = {} for a in set(li): l[a] = 0 for a in li: l[a] = l[a] + 1 print &#8220;The uniq ip number is &#8220;, len(l) return l def apachelog(path):<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-821","post","type-post","status-publish","format-standard","hentry","category-14"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pjq.me\/?p=821\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"\u4e3a\u4e86\u7edf\u8ba1\u6bcf\u4e2aIP\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u507f\u8bd5\u7740\u5199\u4e86\u4e00\u4e2apython\u811a\u672c\uff0c\u5f97\u5230\u524d10\u4e2a\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u7684IP\uff1a def countstatics(result): &quot;&quot;&quot;Compute the statics.&quot;&quot;&quot; li = result.split(&#039;n&#039;) l = {} for a in set(li): l[a] = 0 for a in li: l[a] = l[a] + 1 print &quot;The uniq ip number is &quot;, len(l) return l def apachelog(path):\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pjq.me\/?p=821\" \/>\n<meta property=\"og:site_name\" content=\"Jianqing&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2009-11-15T15:41:50+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\" \/>\n<meta name=\"author\" content=\"pengjianqing\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pengjianqing\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/pjq.me\/?p=821#article\",\"isPartOf\":{\"@id\":\"https:\/\/pjq.me\/?p=821\"},\"author\":{\"name\":\"pengjianqing\",\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60\"},\"headline\":\"Python \u5206\u6790apache log\u811a\u672c\",\"datePublished\":\"2009-11-15T15:41:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/pjq.me\/?p=821\"},\"wordCount\":16,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60\"},\"image\":{\"@id\":\"https:\/\/pjq.me\/?p=821#primaryimage\"},\"thumbnailUrl\":\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\",\"articleSection\":[\"\u81ea\u7531\u5206\u7c7b\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/pjq.me\/?p=821#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/pjq.me\/?p=821\",\"url\":\"https:\/\/pjq.me\/?p=821\",\"name\":\"Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/pjq.me\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/pjq.me\/?p=821#primaryimage\"},\"image\":{\"@id\":\"https:\/\/pjq.me\/?p=821#primaryimage\"},\"thumbnailUrl\":\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\",\"datePublished\":\"2009-11-15T15:41:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/pjq.me\/?p=821#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/pjq.me\/?p=821\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/pjq.me\/?p=821#primaryimage\",\"url\":\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\",\"contentUrl\":\"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/pjq.me\/?p=821#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/pjq.me\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python \u5206\u6790apache log\u811a\u672c\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/pjq.me\/#website\",\"url\":\"https:\/\/pjq.me\/\",\"name\":\"Jianqing&#039;s Blog\",\"description\":\"Thoughts and Future\",\"publisher\":{\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/pjq.me\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60\",\"name\":\"pengjianqing\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/pjq.me\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-02-at-6.10.58-PM.png\",\"contentUrl\":\"https:\/\/pjq.me\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-02-at-6.10.58-PM.png\",\"width\":460,\"height\":752,\"caption\":\"pengjianqing\"},\"logo\":{\"@id\":\"https:\/\/pjq.me\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/pjq.me\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pjq.me\/?p=821","og_locale":"en_US","og_type":"article","og_title":"Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog","og_description":"\u4e3a\u4e86\u7edf\u8ba1\u6bcf\u4e2aIP\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u507f\u8bd5\u7740\u5199\u4e86\u4e00\u4e2apython\u811a\u672c\uff0c\u5f97\u5230\u524d10\u4e2a\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u7684IP\uff1a def countstatics(result): \"\"\"Compute the statics.\"\"\" li = result.split('n') l = {} for a in set(li): l[a] = 0 for a in li: l[a] = l[a] + 1 print \"The uniq ip number is \", len(l) return l def apachelog(path):","og_url":"https:\/\/pjq.me\/?p=821","og_site_name":"Jianqing&#039;s Blog","article_published_time":"2009-11-15T15:41:50+00:00","og_image":[{"url":"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce","type":"","width":"","height":""}],"author":"pengjianqing","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pengjianqing","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pjq.me\/?p=821#article","isPartOf":{"@id":"https:\/\/pjq.me\/?p=821"},"author":{"name":"pengjianqing","@id":"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60"},"headline":"Python \u5206\u6790apache log\u811a\u672c","datePublished":"2009-11-15T15:41:50+00:00","mainEntityOfPage":{"@id":"https:\/\/pjq.me\/?p=821"},"wordCount":16,"commentCount":1,"publisher":{"@id":"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60"},"image":{"@id":"https:\/\/pjq.me\/?p=821#primaryimage"},"thumbnailUrl":"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce","articleSection":["\u81ea\u7531\u5206\u7c7b"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pjq.me\/?p=821#respond"]}]},{"@type":"WebPage","@id":"https:\/\/pjq.me\/?p=821","url":"https:\/\/pjq.me\/?p=821","name":"Python \u5206\u6790apache log\u811a\u672c - Jianqing&#039;s Blog","isPartOf":{"@id":"https:\/\/pjq.me\/#website"},"primaryImageOfPage":{"@id":"https:\/\/pjq.me\/?p=821#primaryimage"},"image":{"@id":"https:\/\/pjq.me\/?p=821#primaryimage"},"thumbnailUrl":"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce","datePublished":"2009-11-15T15:41:50+00:00","breadcrumb":{"@id":"https:\/\/pjq.me\/?p=821#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pjq.me\/?p=821"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pjq.me\/?p=821#primaryimage","url":"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce","contentUrl":"http:\/\/img.zemanta.com\/pixy.gif?x-id=b25e6a07-8df9-8a37-951d-367ef537afce"},{"@type":"BreadcrumbList","@id":"https:\/\/pjq.me\/?p=821#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pjq.me\/"},{"@type":"ListItem","position":2,"name":"Python \u5206\u6790apache log\u811a\u672c"}]},{"@type":"WebSite","@id":"https:\/\/pjq.me\/#website","url":"https:\/\/pjq.me\/","name":"Jianqing&#039;s Blog","description":"Thoughts and Future","publisher":{"@id":"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pjq.me\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pjq.me\/#\/schema\/person\/0eb1e72d1e69fbbd9b5c0bfd8e2aae60","name":"pengjianqing","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pjq.me\/#\/schema\/person\/image\/","url":"https:\/\/pjq.me\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-02-at-6.10.58-PM.png","contentUrl":"https:\/\/pjq.me\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-02-at-6.10.58-PM.png","width":460,"height":752,"caption":"pengjianqing"},"logo":{"@id":"https:\/\/pjq.me\/#\/schema\/person\/image\/"},"url":"https:\/\/pjq.me\/?author=1"}]}},"views":28633,"_links":{"self":[{"href":"https:\/\/pjq.me\/index.php?rest_route=\/wp\/v2\/posts\/821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pjq.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pjq.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pjq.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pjq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=821"}],"version-history":[{"count":0,"href":"https:\/\/pjq.me\/index.php?rest_route=\/wp\/v2\/posts\/821\/revisions"}],"wp:attachment":[{"href":"https:\/\/pjq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pjq.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pjq.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}