{"id":5349,"date":"2022-11-01T00:49:00","date_gmt":"2022-10-31T16:49:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5349"},"modified":"2022-10-31T11:33:37","modified_gmt":"2022-10-31T03:33:37","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_14-2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5349.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_14"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6211\u73b0\u5728\u7684\u5de5\u4f5c\u5185\u5bb9\u4e00\u822c\u4e0d\u6d89\u53ca\u5230\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u4f46\u5de5\u4f5c\u4e2d\u4e5f\u5c11\u4e0d\u4e86\u4e00\u4e9b\u9700\u8981\u81ea\u52a8\u5316\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u7684\u5185\u5bb9\uff0c\u6240\u4ee5\u591a\u5c11\u8fd8\u4f1a\u5199\u4e00\u4e9b\u4ee3\u7801\u3002Golang\u73b0\u5728\u5f88\u706b\uff0c\u800c\u4e14\u4e4b\u524d\u4e5f\u5b66\u8fc7\u4e00\u6bb5\u65f6\u95f4\uff0c\u4f46\u662f\u5bf9\u4e8e\u5b57\u7b26\u4e32\u5904\u7406\u548c\u7f51\u7edc\u8bf7\u6c42\u7684\u5de5\u4f5c\uff0c\u8fd8\u662f\u66f4\u4e60\u60ef\u7528Python\u6765\u5b9e\u73b0\uff08\u7528Java\u4e5f\u80fd\u5b8c\u6210\uff0c\u4f46\u662f\u4e5f\u4e0d\u592a\u719f\uff0c\u800c\u4e14\u5b9e\u73b0\u8d77\u8fd9\u4e9b\u529f\u80fd\u6765\u8bf4\u7565\u663e\u7b28\u91cd\uff09\u3002\u7b80\u5355\u8bb0\u5f55\u6574\u7406\u4e00\u4e0b\u6700\u8fd1\u78b0\u5230\u7684\u51e0\u4e2aPython\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\uff08\u624d\u53d1\u73b0\u4e0a\u4e00\u6b21\u8bb0\u5f55\u8fd9\u4e2a\u4e3b\u9898\u8fd8\u662f\u57282019\u5e745\u670814\u65e5\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<ol class=\"wp-block-list\"><li>\u7528Python\u5f80Kafka\u53d1\u6d88\u606f<\/li><\/ol>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install kafka-python<\/code><\/pre>\n\n\n\n<p>\u751f\u4ea7\u6d88\u606f\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\nfrom kafka import KafkaProducer\n\n# bootstrap = '192.168.3.4:9092'\nbootstrap = &#91;'192.168.3.4:9092', '192.168.3.5:9092', '192.168.3.6:9092']\nproducer = KafkaProducer(bootstrap_servers=bootstrap)\n\ninfo_dict = {\"timestr\":\"2022-10-24 01:02:03\", \"msg_type\":\"alert\", \"ip\":\"1.2.3.4\", \"user_ip\":\"sec-test\", \"user_ip_org\":\"\/a\/b\/c\", \"user_sso\":\"user1\", \"user_sso_org\":\"\/a\/b\/d\", \"extra_info\":\"\"}\n\nfor idx in range(5):\n    print(idx)\n    producer.send('my_favorite_topic_name', json.dumps(info_dict).encode('utf-8'))\n#for\nproducer.flush()<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1. \u53d1\u5e03\u6d88\u606f\u7684send\u65b9\u6cd5\u7684\u7b2c2\u4e2a\u53c2\u6570\uff0c\u5373\u8981\u53d1\u9001\u7684\u6d88\u606f\u5185\u5bb9value\u9700\u8981\u662fbytes\u5b57\u8282\u7c7b\u578b\u7684\nproducer.send(topic, value=None, key=None, headers=None, partition=None, timestamp_ms=None)\n\n\u6240\u4ee5\u5f53\u4f60\u8981\u53d1\u9001\u7684\u662fjson\u5b57\u7b26\u4e32\u65f6\uff0c\u8981\u4e48\u5c31\u662f\u5728\u65b0\u5efa KafkaProducer \u65f6\u901a\u8fc7 value_serializer \u53c2\u6570\u6765\u81ea\u52a8\u7f16\u7801\uff0c\u8981\u4e48\u5c31\u662f\u624b\u52a8\u5728\u6bcf\u4e00\u4e2a send \u91cc\u9762\u5bf9\u8981\u53d1\u9001\u7684 value \u5185\u5bb9\u8fdb\u884c\u7f16\u7801\u5904\u7406\u3002\n\n&gt;&gt;&gt; import json\n&gt;&gt;&gt; info_dict = {\"timestr\":\"2022-10-24 01:02:03\", \"msg_type\":\"alert\"}\n\n# \u65b9\u6cd5\u4e00\n&gt;&gt;&gt; producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'))\n&gt;&gt;&gt; producer.send('fizzbuzz', info_dict)\n\n# \u65b9\u6cd5\u4e8c\n&gt;&gt;&gt; producer = KafkaProducer()\n&gt;&gt;&gt; producer.send('fizzbuzz', json.dumps(info_dict).encode('utf-8'))\n\n2. \u6d4b\u8bd5\u7684\u65f6\u5019\u5982\u679c send \u4e4b\u540e\u4f46\u662f topic \u91cc\u6ca1\u6d88\u606f\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8 flush \u624d\u884c\n&gt;&gt;&gt; producer.flush()<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>Python\u4e2d dict \u7684\u5904\u7406<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5c06 dict \u6309json\u5b57\u7b26\u4e32\u683c\u5f0f\u5199\u6587\u4ef6\nimport json\nexDict = {'exDict': 'test'}\nwith open('mydict_file1.txt', 'w') as fp:\n     fp.write(json.dumps(exDict)) # use `json.loads` to do the reverse\n\n# Python 3 \u6d4b\u8bd5\u53ef\u884c\uff0c\u4f46\u662f\u5199\u5165\u7684\u5185\u5bb9\u4e0d\u662f\u6807\u51c6\u7684json\u683c\u5f0f\uff0c\u7528\u7684\u662f\u5355\u5f15\u53f7\u800c\u975e\u53cc\u5f15\u53f7\nwith open('mydict_file2.txt', 'w') as fp:\n    print(exDict, file=fp)<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Python\u4e2d\u4f7f\u7528 APScheduler \u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1<\/li><\/ol>\n\n\n\n<p>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip install apscheduler<\/code><\/pre>\n\n\n\n<p>\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5f53\u4f60\u7684\u7a0b\u5e8f\u53ea\u662f\u7528\u6765\u6267\u884c\u8c03\u5ea6\u65f6\u53ef\u4ee5\u4f7f\u7528 BlockingScheduler \uff0c\u5b83\u4f1a\u4ee5\u963b\u585e\u7684\u65b9\u5f0f\u4e00\u76f4\u8fd0\u884c\uff0c\u4ee5\u4fbf\u5b9a\u671f\u6267\u884c\u4efb\u52a1\u8c03\u5ea6\nfrom datetime import datetime\nfrom apscheduler.schedulers.blocking import BlockingScheduler\n\nn_seconds = 3\n\ndef my_clock():\n    print('time is {0}'.format(datetime.now()))\n\nif __name__ == \"__main__\":\n    scheduler = BlockingScheduler()\n    scheduler.add_job(my_clock, trigger=\"interval\", seconds=n_seconds, next_run_time=datetime.now())\n    scheduler.start()\n#if\n\n\n# \u5f53\u4f60\u4e0d\u4f7f\u7528 Tornado\/Twisted\/asyncio\/gevent \u7b49\u6846\u67b6\u6216\u6a21\u5757\uff0c\u53c8\u5e0c\u671b\u8c03\u5ea6\u7a0b\u5e8f\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u540e\u53f0\u8fd0\u884c\u65f6\u4f7f\u7528 BackgroundScheduler \uff0c\u4f46\u662f\u4f60\u81ea\u5df1\u7684\u7a0b\u5e8f\u9700\u8981\u4e00\u76f4\u5728\u7ebf\uff0c\u5426\u5219\u4e3b\u7a0b\u5e8f\u9000\u51fa\u4e86\u8c03\u5ea6\u4efb\u52a1\u80af\u5b9a\u4e5f\u5c31\u65e0\u6cd5\u6b63\u5e38\u6267\u884c\u4e86\nfrom datetime import datetime\nfrom apscheduler.schedulers.blocking import BlockingScheduler\n\nfrom apscheduler.schedulers.background import BackgroundScheduler\ndef job1():\n    print('job1: time is {0}'.format(datetime.now()))\n    with open('test_BackgroundScheduler.txt', 'a') as fp:\n        fp.write('job1: time is {0}\\n'.format(datetime.now()))\n    #with\n\nif __name__ == \"__main__\":\n    scheduler = BackgroundScheduler()\n    scheduler.add_job(job1, trigger='cron', second='*\/10')\n    scheduler.start()\n    while 1: #\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2a\u7c7b\u4f3cwhile\u5faa\u73af\u7684\u529f\u80fd\u4fdd\u8bc1\u4e3b\u7a0b\u5e8f\u5728\u7ebf\uff0c\u5426\u5219\u4efb\u52a1\u65e0\u6cd5\u6309\u7167\u9884\u671f\u8fdb\u884c\u8c03\u5ea6\n        time.sleep(5)\n    #while\n#if<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>Python\u4e2d\u4f7f\u7528 elasticsearch5 \u6a21\u5757\u8fdb\u884cES\u7684\u67e5\u8be2<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># \u67e5\u8be2body\u53ef\u4ee5\u901a\u8fc7\u5728\u9875\u9762\u4e0a\u7528Kibana\u65f6\uff0c\u901a\u8fc7\u6d4f\u89c8\u5668\u63a7\u5236\u53f0\u6293\u5305\u770b\u770bRequest Payload\u6765\u5feb\u901f\u83b7\u53d6\nfrom elasticsearch5 import Elasticsearch\nes = Elasticsearch(hosts=\"10.20.30.40:9200\", timeout=60)\n\nbody = {\n    # \"key\": \"value\"\n}\n\nresult = es.search(index=\"index_name-*\", body=body) #search()\u8fd4\u56de\u7684\u662f dict \u7c7b\u578b\nhits = result.get('hits')\nshards = result.get('_shards')\nprint('{0}\\n{1}'.format(hits, shards))\n\nimport jsonpath\ndata = jsonpath.jsonpath(result, \"$.aggregations.aggr_name.buckets&#91;*]\")<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>kafka-python<br><a href=\"https:\/\/kafka-python.readthedocs.io\/en\/master\/index.html#kafkaproducer\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/kafka-python.readthedocs.io\/en\/master\/index.html#kafkaproducer<\/a><br><a href=\"https:\/\/kafka-python.readthedocs.io\/en\/master\/apidoc\/KafkaProducer.html#kafka.KafkaProducer.send\">https:\/\/kafka-python.readthedocs.io\/en\/master\/apidoc\/KafkaProducer.html#kafka.KafkaProducer.send<\/a><\/p>\n\n\n\n<p>Unable to send messages to topic in Kafka Python<br><a href=\"https:\/\/stackoverflow.com\/questions\/62405458\/unable-to-send-messages-to-topic-in-kafka-python\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/62405458\/unable-to-send-messages-to-topic-in-kafka-python<\/a><\/p>\n\n\n\n<p>Writing a dictionary to a text file?<br><a href=\"https:\/\/stackoverflow.com\/questions\/36965507\/writing-a-dictionary-to-a-text-file\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/36965507\/writing-a-dictionary-to-a-text-file<\/a><\/p>\n\n\n\n<p>APScheduler User guide<br><a href=\"https:\/\/apscheduler.readthedocs.io\/en\/3.x\/userguide.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/apscheduler.readthedocs.io\/en\/3.x\/userguide.html<\/a><\/p>\n\n\n\n<p>Python \u5b9a\u65f6\u4efb\u52a1\u6846\u67b6 APScheduler \u8be6\u89e3<br><a href=\"https:\/\/www.cnblogs.com\/leffss\/p\/11912364.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.cnblogs.com\/leffss\/p\/11912364.html<\/a><\/p>\n\n\n\n<p>python BlockingScheduler\u5b9a\u65f6\u4efb\u52a1<br><a href=\"https:\/\/blog.csdn.net\/lipachong\/article\/details\/99962134\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.csdn.net\/lipachong\/article\/details\/99962134<\/a><\/p>\n\n\n\n<p>elasticsearch5 5.5.6<br><a href=\"https:\/\/pypi.org\/project\/elasticsearch5\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pypi.org\/project\/elasticsearch5\/<\/a><br><a href=\"https:\/\/elasticsearch-py.readthedocs.io\/en\/v7.17.7\/#example-usage\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/elasticsearch-py.readthedocs.io\/en\/v7.17.7\/#example-usage<\/a><\/p>\n\n\n\n<p>ElasticSearch\u7684scroll\u63a5\u53e3\u641c\u7d22\u793a\u4f8b<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/4112.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/ixyzero.com\/blog\/archives\/4112.html<\/a><\/p>\n\n\n\n<p>JSONPath &#8211; XPath for JSON<br><a href=\"https:\/\/goessner.net\/articles\/JsonPath\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/goessner.net\/articles\/JsonPath\/<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/pypi.org\/project\/jsonpath\/\">https:\/\/pypi.org\/project\/jsonpath\/<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6211\u73b0\u5728\u7684\u5de5\u4f5c\u5185\u5bb9\u4e00\u822c\u4e0d\u6d89\u53ca\u5230\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u4f46\u5de5\u4f5c\u4e2d\u4e5f\u5c11\u4e0d\u4e86\u4e00\u4e9b\u9700\u8981\u81ea\u52a8\u5316\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1866,220,1207,1865,10,8,1809],"class_list":["post-5349","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-apscheduler","tag-dict","tag-elasticsearch","tag-jsonpath","tag-kafka","tag-python","tag-xpath"],"views":1791,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5349","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5349"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5349\/revisions"}],"predecessor-version":[{"id":5350,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5349\/revisions\/5350"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}