Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
devwiki:python [2023/07/07 07:35] – [Manage and handle multiple versions of python case] ying | devwiki:python [2024/03/25 06:36] (current) – [Python and interaction with other API] ying | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | ====== Modern Python Practice ====== | ||
+ | |||
+ | * nowaday, now one use python 2.x anymore, use python 3 standard, use this | ||
+ | * use pathlib instead of os.path | ||
+ | * use f" | ||
+ | |||
+ | |||
+ | ====== Online Python run ====== | ||
+ | |||
+ | * https:// | ||
+ | * to install locally: < | ||
====== Install related ====== | ====== Install related ====== | ||
Line 932: | Line 944: | ||
except OSError: | except OSError: | ||
pass | pass | ||
+ | | ||
+ | # method 3: all in one check and create if needed, like method 2 but not need try and except since it will be ok for exist | ||
+ | os.makedirs(os.path.dirname(output_path), | ||
</ | </ | ||
* write python data< | * write python data< | ||
Line 1350: | Line 1365: | ||
result.text # is html in unicode text | result.text # is html in unicode text | ||
</ | </ | ||
+ | * disable non secure cert warning <code python> | ||
===== PIL and Pillow===== | ===== PIL and Pillow===== | ||
Line 2365: | Line 2380: | ||
send_button.click() | send_button.click() | ||
</ | </ | ||
+ | |||
+ | ====== Python and interaction with other API ====== | ||
+ | |||
+ | Telegram | ||
+ | - search @BotFather on telegram | ||
+ | * manual: https:// | ||
+ | - in the chat, type /newbot, then type your bot " | ||
+ | - now, your bot api key will show up, copy and save it | ||
+ | - now search your bot " | ||
+ | - in python, use requests to get the chat from the bot, try one more time if no result <code python> | ||
+ | import requests | ||
+ | the_key = " | ||
+ | url = " | ||
+ | response = requests.get(url) | ||
+ | result = response.json() | ||
+ | print(result) | ||
+ | </ | ||
+ | - once you got result in json, you will find the chat id. <code python> | ||
+ | # the list of all chat spec your bot received, yours is likely the first one. | ||
+ | my_chat_info = result[' | ||
+ | my_msg_info = my_chat_info[0][' | ||
+ | # it got info of: chat, date, from, message_id, text | ||
+ | my_chat_id = my_msg_info[' | ||
+ | </ | ||
+ | - to send a text to that chat from your bot side <code python> | ||
+ | reply_text = 'hello world' | ||
+ | url = " | ||
+ | response = requests.get(url) | ||
+ | </ | ||
+ | - now, you should have got the reply from your bot <code python> | ||
+ | result = response.json() | ||
+ | send_state = result[' | ||
+ | send_result = result[' | ||
+ | # chat (first_name, | ||
+ | </ | ||
+ | |||
+ | ====== Python and Networking related ====== | ||
+ | |||
+ | * above is python interact with a REST API server, you can also use python to build your own REST API server to handle requests from other application, | ||
+ | * implement REST API into python, you may need to use some python module like Flash, FastAPI, then you start another thread from your python application to run your REST API server. | ||
+ | * while using Sockets method may be simpler if you just want to send some simple message between applications, | ||
+ | * using REST API allow your application to talk cross different application and difference device, and provide a safe gate to your important data handled by your application. | ||
+ |