英文:
Configparser How to Use a URL?
问题
import configparser
configini = configparser.ConfigParser()
configini.read("scr/config/config.ini")
#show and print
Device_Name = configini.get('DeviceInfo', 'Devicename')
print(Device_Name)
TGtoken = Device_Name
这个部分代码是正确的,它从配置文件中读取了设备名称,并将其赋值给了变量 TGtoken。如果你想要进一步简化代码,你可以直接使用以下代码来获取设备名称:
TGtoken = configini.get('DeviceInfo', 'Devicename')
英文:
i try to creat a config.ini file i inserted this and want to read it and write this inside a variable for curl and for getting a name of the device
Config.ini
[DEFAULT]
#only default infos DONT CHANGE
DevicenameDEFAULT = NameoftheDevice
TGtokenCurlDEFAULT = TokenUrlforCUrltoTelegram
[DeviceInfo]
#change here your settings
Devicename = miniforum1-titty
TGtokenCurl = https://api.telegram.org/botxxxxxxx&text=
and want to use this with a variable
Geraet =
TGtoken =
and use the TGtoken inside this code:
def CurlExeptionErrorAccours():
sleep(2)
subprocess.call(['curl',
'-X',
'POST',
'-d',
'flow_x',
(TGtoken,'ExeptionErrorAccours_',Geraet)])
#Time for Timestamps
timenow = datetime.datetime.now()
print("Curl ExeptionErrorAccours", timenow)
sleep(1)
pass
and my script (function) looks like this:
import configparser
configini = configparser.ConfigParser()
configini.read("scr\config\config.ini")
#show and print
Device_Name = configini.get('DeviceInfo', 'Devicename')
print(Device_Name)
TGtoken = Device_Name
is it right like this or should i use
TGtoken = configini.get('DeviceInfo', 'Devicename')
答案1
得分: 1
Configparser 只使用字符串。URL 只是一个字符串。
你似乎只是在询问有关变量的问题。因此,首先我建议你使用参数:
def CurlExeptionErrorAccours(url, Geraet):
subprocess.call(['curl', # 不过,你应该用本机的 Python HTTP 客户端调用来替代这里的命令
'-X',
'POST',
'-d',
'flow_x',
(url, 'ExeptionErrorAccours_', Geraet)])
# TODO: 你应该检查此命令的退出代码以查找失败
然后:
```python
import configparser
configini = configparser.ConfigParser()
configini.read("scr/config/config.ini")
# 展示和打印
url = configini.get('DeviceInfo', 'TGtokenCurl')
Geraet = configini.get('DeviceInfo', 'Devicename') # 德语翻译?为什么不把 Geraet 作为配置键呢?
CurlExeptionErrorAccours(url, Geraet)
英文:
Configparser only uses strings. URL is just a String.
You seem to be simply be asking about variables. So, first I suggest you use parameters
def CurlExeptionErrorAccours(url, Geraet):
subprocess.call(['curl', # however, you should replace this with native python http client call
'-X',
'POST',
'-d',
'flow_x',
(url,'ExeptionErrorAccours_',Geraet)])
# TODO: you should check the exit code of this command for failures
Then
import configparser
configini = configparser.ConfigParser()
configini.read("scr\config\config.ini")
#show and print
url = configini.get('DeviceInfo', 'TGtokenCurl')
Geraet = configini.get('DeviceInfo', 'Devicename') # German translation? Why not put Geraet as config key?
CurlExeptionErrorAccours(url, Geraet)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论