請問各位大大,想寫一個判別5次後推播要怎寫
但小弟想要多偵測幾次真失敗 再推播 請問要如何修改?
以下是小弟目前的程式碼 只要偵測到失敗一次就推播
def plus():
while True:
GE = test0.get()
name1 = test1.get()
ht2 = test2.get()
url1 = ht2
name3 = test3.get()
ht4 = test4.get()
url2 = ht4
name5 = test5.get()
ht6 = test6.get()
url3 = ht6
name7 = test7.get()
ht8 = test8.get()
url4 = ht8
try:
html1 = requests.get(url1, timeout=5).text
a = str(name1)+'連接正常'
ret1_msg.set(a)
except:
a = str(name1)+'連接失敗'
ret1_msg.set(a)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message1 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data1 = { 'message': message1 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data1)
try:
html2 = requests.get(url2, timeout=5).text
b = str(name3)+'連接正常'
ret2_msg.set(b)
except:
b = str(name3)+'連接失敗'
ret2_msg.set(b)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message2 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data2 = { 'message': message2 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data2)
try:
html3 = requests.get(url3, timeout=5).text
c = str(name5)+'連接正常'
ret3_msg.set(c)
except:
c = str(name5)+'連接失敗'
ret3_msg.set(c)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message3 = c
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data3 = { 'message': message3 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data3)
try:
html4 = requests.get(url4, timeout=5).text
d = str(name7)+'連接正常'
ret4_msg.set(d)
time.sleep(60)
except:
d = str(name7)+'連接失敗'
ret4_msg.set(d)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message4 = d
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data4 = { 'message': message4 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data4)
time.sleep(60)
隨便寫ㄉ
def tryToGet(url,flag = 5):
result = ""
while flag:
try:
result = request.get(url,timeout=5).text
break
except:
flag -=1
result=""
time.sleep(3)
return result
html1 = tryToGet(url,5)
if html1:
a = str(name1) + '連接正常'
ret1_msg.set(a)
else:
就填寫你要的失敗內容
以上程式碼會出現此段錯誤 missing 1 required positional argument: 'url'
jimmy8667104代表你參數沒放對
tryit我將程式修改成這樣
import time
import requests
name1 = input("名稱")
url = input("網址")
def tryToGet(url, flag = 5):
result = ""
print(result)
while flag:
try:
result = request.get(url,timeout=5).text
break
except:
flag -=1
result=""
time.sleep(3)
return result
html1 = tryToGet(url,5)
if html1:
a = str(name1) + '連接正常'
print(a)
else:
a = str(name1) + '連接失敗'
print(a)
可以正常運行卻說連接失敗,但網址是正確的也沒斷線
jimmy8667104
你可以在except裡面添加exception 把錯誤內容print出來看看
tryit可以了 感謝您
這容我問幾個問題
如果我要永久去判斷這個網址
就是偵測網址不會變一直都一樣要怎做?
我在if和else最後加上time.sleep(60)都無法永久執行
import time
import requests
def tryToGet(flag = 5):
url = input("網址")
name1 = input("名稱")
result=""
while flag:
try:
result = requests.get(url,timeout=5).text
break
except:
flag -=1
result=""
time.sleep(3)
return result
html1 = tryToGet(5)
if html1:
a = '連接正常'
print(a)
else:
a = '連接失敗'
print(a)
time.sleep(60)
還有一點就是我有多網址要偵測
是一定要開好幾隻?
還是可以用一個內部套用多個while、try、except、if、else嗎?
我試過會跟我說架構錯誤
#迴圈
def plus(flag = 5):
GE = test0.get()
name1 = test1.get()
name3 = test3.get()
name5 = test5.get()
name7 = test7.get()
ht2 = test2.get()
url1 = ht2
ht4 = test4.get()
url2 = ht4
ht6 = test6.get()
url3 = ht6
ht8 = test8.get()
url4 = ht8
result1=""
result2=""
result3=""
result4=""
while flag:
try:
result1 = requests.get(url1,timeout=5).text
break
except:
flag -=1
result1=""
time.sleep(3)
return result1
html1 = tryToGet(5)
if html1:
a =str(name1)+'連接正常'
ret1_msg.set(a)
else:
a =str(name1)+'連接失敗'
ret1_msg.set(a)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message1 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data1 = { 'message': message1 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data1)
try:
result2 = requests.get(url2, timeout=5).text
break
except:
flag -=1
result2=""
time.sleep(3)
return result2
html2 = tryToGet(5)
if html1:
b = str(name3)+'連接正常'
ret2_msg.set(b)
else:
b = str(name3)+'連接失敗'
ret2_msg.set(b)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message2 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data2 = { 'message': message2 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data2)
會出現 unexpected indent 是我要在try前加上while flag:嗎?
這樣才不會造成架構錯誤?
jimmy8667104
我的話要不就hardcode,直接把網址丟在程式碼內,要不然就是寫個文件,利用文件進行記錄網址的動作。
執行時讀取文件進行程式即可
然後你的程式碼,恩.....
def tryToGet(url,flag = 5):
result = ""
while flag:
try:
result = request.get(url,timeout=5).text
break
except:
flag -=1
result=""
time.sleep(3)
return result
def plus():
GE = test0.get()
name1 = test1.get()
ht2 = test2.get()
url1 = ht2
name3 = test3.get()
ht4 = test4.get()
url2 = ht4
name5 = test5.get()
ht6 = test6.get()
url3 = ht6
name7 = test7.get()
ht8 = test8.get()
url4 = ht8
urlList = {url1:name1,url2:name2,url3:name3,url4:name4}
for i in urlList:
result = tryToGet(i,5)
if result:
a = str(name1)+'連接正常'
ret1_msg.set(a)
else:
a = str(name1)+'連接失敗'
# LINE Notify 權杖
token = GE
# 要發送的訊息
message1 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data1 = { 'message': message1 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data1)
總之你的東西很亂,我也沒你全部東西的資料,老實說你要幹啥我也不知道,個人是覺得你程式寫複雜了,我能夠幫你簡化的大概這樣,後續要怎麼處理要再思考
tryit感謝您
我也覺得我寫得很雜亂
有想過全部寫完再來簡化
目前這支程式是要偵測攝影機網址的
如果當網址死亡後會自動推流一支影片上YOUTOBE去直播影片
同時也會以賴訊息通知管理員
因為我有使用GUI元件 所以有寫程式碼可能套用到GUI上會要修改
目前程式碼整段程式碼是這樣
import tkinter as tk
from tkinter.constants import CENTER
from tkinter import messagebox
from tkinter import ttk
from tkinter import *
import subprocess
import sys
import time
import requests
import os
import threading
from tkinter import filedialog
window = tk.Tk()
window.title('song ping')
window.geometry('400x600')
window.resizable(False, False)
window.iconbitmap("logo.ico")
ret1_msg = tk.StringVar() # 回傳值#1
ret2_msg = tk.StringVar() # 回傳值#2
ret3_msg = tk.StringVar() # 回傳值#3
# 設定回應值#1
ret1_label = tk.Label(window, textvariable=ret1_msg, foreground="red", font=("標楷體", 12), padx=30, pady=20)
ret1_label.place(x=200,y=60,anchor=CENTER)
# 設定回應值#2
ret2_labe2 = tk.Label(window, textvariable=ret2_msg, foreground="red", font=("標楷體", 12), padx=30, pady=20)
ret2_labe2.place(x=200,y=150,anchor=CENTER)
# 設定回應值#3
ret3_labe3 = tk.Label(window, textvariable=ret3_msg, foreground="red", font=("標楷體", 12), padx=30, pady=20)
ret3_labe3.place(x=200,y=240,anchor=CENTER)
la0= tk.Label (text="LINE金鑰:")
la0.place(x=50,y=30,anchor=CENTER)
test0 = tk.Entry(width=40,show="")
test0.place(x=230,y=30,anchor=CENTER)
la1= tk.Label (text="偵測位置:")
la1.place(x=50,y=90,anchor=CENTER)
test1 = tk.Entry(width=40,show="")
test1.place(x=230,y=90,anchor=CENTER)
la2= tk.Label (text="偵測網址:")
la2.place(x=50,y=120,anchor=CENTER)
test2 = tk.Entry(width=40,show="")
test2.place(x=230,y=120,anchor=CENTER)
la3= tk.Label (text="偵測位置:")
la3.place(x=50,y=180,anchor=CENTER)
test3 = tk.Entry(width=40,show="")
test3.place(x=230,y=180,anchor=CENTER)
la4= tk.Label (text="偵測網址:")
la4.place(x=50,y=210,anchor=CENTER)
test4 = tk.Entry(width=40,show="")
test4.place(x=230,y=210,anchor=CENTER)
la5= tk.Label (text="偵測位置:")
la5.place(x=50,y=270,anchor=CENTER)
test5 = tk.Entry(width=40,show="")
test5.place(x=230,y=270,anchor=CENTER)
la6= tk.Label (text="偵測網址:")
la6.place(x=50,y=300,anchor=CENTER)
test6 = tk.Entry(width=40,show="")
test6.place(x=230,y=300,anchor=CENTER)
def plus1():
while True:
GE = test0.get()
name1 = test1.get()
ht2 = test2.get()
url1 = ht2
name3 = test3.get()
ht4 = test4.get()
url2 = ht4
name5 = test5.get()
ht6 = test6.get()
url3 = ht6
try:
html1 = requests.get(url1, timeout=5).text
a = str(name1)+'連接正常'
ret1_msg.set(a)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message1 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data1 = { 'message': message1 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data1)
except requests.exceptions.RequestException as e:
a = str(name1)+'連接失敗'
ret1_msg.set(a)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message1 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data1 = { 'message': message1 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data1)
try:
html2 = requests.get(url2, timeout=5).text
b = str(name3)+'連接正常'
ret2_msg.set(b)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message2 = b
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data2 = { 'message': message2 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data2)
except requests.exceptions.RequestException as e:
b = str(name3)+'連接失敗'
ret2_msg.set(b)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message2 = a
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data2 = { 'message': message2 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data2)
try:
html3 = requests.get(url3, timeout=5).text
c = str(name5)+'連接正常'
ret3_msg.set(c)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message3 = c
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data3 = { 'message': message3 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data3)
break
except requests.exceptions.RequestException as e:
c = str(name5)+'連接失敗'
ret3_msg.set(c)
# LINE Notify 權杖
token = GE
# 要發送的訊息
message3 = c
# HTTP 標頭參數與資料
headers = { "Authorization": "Bearer " + token }
data3 = { 'message': message3 }
# 以 requests 發送 POST 請求
requests.post("https://notify-api.line.me/api/notify", headers = headers, data = data3)
break
#子序列
def loop():
thread = threading.Thread(target=plus1)
thread.start()
def endloop():
while True:
os.system("taskkill /f /im song ping.exe")
break
bu1 = tk.Button(text="開始",command=loop)
bu1.place(x=100,y=570,anchor=CENTER)
bu2 = tk.Button(text="停止",command=endloop)
bu2.place(x=300,y=570,anchor=CENTER)
window.mainloop()
這是還沒套上五次失敗後傳送訊息的及失敗後推流的