python请求post在循环中只执行一次
我试图在循环中执行下面的函数python请求post在循环中只执行一次,python,python-requests,Python,Python Requests,我试图在循环中执行下面的函数 def post_webhook(payload): if not WEBHOOK_URL:
def post_webhook(payload):
if not WEBHOOK_URL:
print "WEBHOOK_URL isn't setted"
return
try:
json = {'data': payload}
print "webhook_url:", WEBHOOK_URL
r = requests.post(WEBHOOK_URL, data=json)
print "requests.post:", r.text
except Exception as e:
print "post_webhook err:", e
像这样
def post_webhook(payload):
if not WEBHOOK_URL:
print "WEBHOOK_URL isn't setted"
return
try:
json = {'data': payload}
print "webhook_url:", WEBHOOK_URL
r = requests.post(WEBHOOK_URL, data=json)
print "requests.post:", r.text
except Exception as e:
print "post_webhook err:", e
while True:
post_webhook(payload)
但是,post_webhook函数只执行一次
当我在while循环中调用post_webook()时,print“requests.post:,r.text
不会在post_webhook()中执行
def post_webhook(payload):
if not WEBHOOK_URL:
print "WEBHOOK_URL isn't setted"
return
try:
json = {'data': payload}
print "webhook_url:", WEBHOOK_URL
r = requests.post(WEBHOOK_URL, data=json)
print "requests.post:", r.text
except Exception as e:
print "post_webhook err:", e
因此,r=requests.post(WEBHOOK\u URL,data=json)
只在循环中执行一次,我想要的是在循环中多次执行post\u WEBHOOK()
def post_webhook(payload):
if not WEBHOOK_URL:
print "WEBHOOK_URL isn't setted"
return
try:
json = {'data': payload}
print "webhook_url:", WEBHOOK_URL
r = requests.post(WEBHOOK_URL, data=json)
print "requests.post:", r.text
except Exception as e:
print "post_webhook err:", e
为什么request.post在循环中只执行一次?一个可能的原因可能是您的请求尚未完成。如果是这种情况,则不应从
r.text
打印任何内容。如果是这种情况,那么让它超时,然后它将再次调用相同的方法。在它打印一行输出后会发生什么?它是退出还是挂起?@hyades我怎么能让它超时?@1.618不退出,它挂起只是将timeout=5
设置为参数之一。参考
def post_webhook(payload):
if not WEBHOOK_URL:
print "WEBHOOK_URL isn't setted"
return
try:
json = {'data': payload}
print "webhook_url:", WEBHOOK_URL
r = requests.post(WEBHOOK_URL, data=json)
print "requests.post:", r.text
except Exception as e:
print "post_webhook err:", e