Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python请求post在循环中只执行一次_Python_Python Requests - Fatal编程技术网

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