{"id":1722,"date":"2014-12-22T14:04:33","date_gmt":"2014-12-22T14:04:33","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1722"},"modified":"2014-12-22T14:04:33","modified_gmt":"2014-12-22T14:04:33","slug":"%e4%bd%bf%e7%94%a8python%e8%ae%a1%e7%ae%97tcp%e5%8c%85%e4%b8%ad%e7%9a%84%e6%a0%a1%e9%aa%8c%e5%92%8c","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1722.html","title":{"rendered":"\u4f7f\u7528Python\u8ba1\u7b97TCP\u5305\u4e2d\u7684\u6821\u9a8c\u548c"},"content":{"rendered":"<p>\u9996\u5148\u786e\u5b9a\u641c\u7d22\u5173\u952e\u5b57\uff1ause python to verify TCP Checksum<\/p>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=use+python+to+verify+TCP+Checksum\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=use+python+to+verify+TCP+Checksum<\/a><\/p>\n<p>\u67e5\u770b\u300aTCP\/IP\u8be6\u89e3\uff1a\u53771\u300b{Page 26}<\/p>\n<p>IP\u6570\u636e\u62a5\u7684\u201c\u9996\u90e8\u6821\u9a8c\u548c\u201d\u5b57\u6bb5\u662f<strong><span style=\"color: #ff0000;\">\u6839\u636eIP\u9996\u90e8\u8ba1\u7b97\u51fa<\/span><\/strong>\u7684\u6821\u9a8c\u548c\uff0c\u5b83\u4e0d\u5bf9\u9996\u90e8\u540e\u9762\u7684\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<p><span style=\"color: #ff0000;\">ICMP\u3001IGMP\u3001UDP\u548cTCP\u5728\u5b83\u4eec\u5404\u81ea\u7684\u9996\u90e8\u4e2d\u5747\u542b\u6709\u540c\u65f6\u8986\u76d6<strong>\u9996\u90e8\u548c\u6570\u636e<\/strong>\u7684\u6821\u9a8c\u548c\u3002<\/span><\/p>\n<p>\u4e3a\u4e86\u8ba1\u7b97\u4e00\u4efd\u6570\u636e\u62a5\u7684IP\u6821\u9a8c\u548c\uff0c\u9996\u5148\u628a\u6821\u9a8c\u548c\u5b57\u6bb5\u7f6e\u4e3a0\u3002\u7136\u540e\uff0c\u5bf9\u9996\u90e8\u4e2d\u6bcf\u4e2a16 bit\u8fdb\u884c\u4e8c\u8fdb\u5236\u53cd\u7801\u6c42\u548c\uff08\u6574\u4e2a\u9996\u90e8\u770b\u6210\u662f\u7531\u4e00\u4e3216 bit\u7684\u5b57\u7ec4\u6210\uff09\uff0c\u7ed3\u679c\u5b58\u5728\u6821\u9a8c\u548c\u5b57\u6bb5\u4e2d\u3002<\/p>\n<p><span style=\"color: #ff0000;\">ICMP\u3001IGMP\u3001UDP\u548cTCP\u90fd\u91c7\u7528\u76f8\u540c\u7684\u6821\u9a8c\u548c\u7b97\u6cd5<\/span>\uff0c\u5c3d\u7ba1TCP\u548cUDP\u9664\u4e86\u672c\u8eab\u7684\u9996\u90e8\u548c\u6570\u636e\u5916\uff0c\u5728IP\u9996\u90e8\u4e2d\u8fd8\u5305\u542b\u4e0d\u540c\u7684\u5b57\u6bb5\u3002\u5728<a href=\"http:\/\/www.ietf.org\/rfc\/rfc1071.txt\">RFC 1071<\/a>\u4e2d\u6709\u5173\u4e8e\u5982\u4f55\u8ba1\u7b97Internet\u68c0\u9a8c\u548c\u7684\u5b9e\u73b0\u6280\u672f\uff1b\u5728<a href=\"http:\/\/www.ietf.org\/rfc\/rfc1141.txt\">RFC 1141<\/a>\u4e2d\u6709\u4e00\u4e2a\u5f88\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528Python\u5b9e\u73b0\uff1a<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\n\nimport struct\n\ndata = \"45 00 00 47 73 88 40 00 40 06 a2 c4 83 9f 0e 85 83 9f 0e a1\"\n\ndef carry_around_add(a, b):\n    c = a + b\n    return (c &amp; 0xffff) + (c &gt;&gt; 16)\n\ndef checksum(msg):\n    s = 0\n    for i in range(0, len(msg), 2):\n        w = ord(msg[i]) + (ord(msg[i+1]) &lt;&lt; 8)\n        s = carry_around_add(s, w)\n    return ~s &amp; 0xffff\n\ndata = data.split()\ndata = map(lambda x: int(x,16), data)\ndata = struct.pack(\"%dB\" % len(data), *data)\n\nprint ' '.join('%02X' % ord(x) for x in data)\nprint \"Checksum: 0x%04x\" % checksum(data)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3949726\/calculate-ip-checksum-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3949726\/calculate-ip-checksum-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1767910\/checksum-udp-calculation-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1767910\/checksum-udp-calculation-python<\/a><\/li>\n<li><a href=\"http:\/\/www.faqs.org\/rfcs\/rfc1071.html\" target=\"_blank\">RFC 1071 &#8211; Computing the Internet checksum (RFC1071)<\/a><\/li>\n<li><a href=\"http:\/\/www.ietf.org\/rfc\/\" target=\"_blank\">http:\/\/www.ietf.org\/rfc\/<\/a>\u00a0 #IETF\u7684RFC<\/li>\n<li><a href=\"http:\/\/www.serkey.com\/tag\/python-calculate-tcp-checksum\/\" target=\"_blank\">http:\/\/www.serkey.com\/tag\/python-calculate-tcp-checksum\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u786e\u5b9a\u641c\u7d22\u5173\u952e\u5b57\uff1ause python to verify TCP Checksum http:\/\/sear [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[425,8,413],"class_list":["post-1722","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-checksum","tag-python","tag-tcpip"],"views":5150,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1722","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=1722"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1722\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}