小编pos*_*nim的帖子

状态码500不被视为异常

向服务器发出请求,如下面的代码所示,我有状态码500,它没有被捕获为异常。输出为“ 500”,但我需要所有500个代码才能生成sys.exit()。request.exceptions.RequestException不会将500视为异常还是其他东西?请求模块文档http://docs.python-requests.org/en/latest/user/quickstart/#errors-and-exceptions对于此类中的内容不是很清楚。如何确保所有500个代码都生成sys.exit()?

import requests
import json
import sys

url = http://www.XXXXXXXX.com
headers = {'user':'me'}
try:
    r = requests.post(url, headers=headers)
    status = r.status_code
    response = json.dumps(r.json(), sort_keys=True, separators=(',', ': '))
    print status
except requests.exceptions.RequestException as e:
    print "- ERROR - Web service exception, msg = {}".format(e)
    if r.status_code < 500:
        print r.status_code
    else:
        sys.exit(-1)
Run Code Online (Sandbox Code Playgroud)

python error-handling http python-requests

4
推荐指数
3
解决办法
4712
查看次数

Python不接受某些数字输入

使用此代码,我想要做的就是在偶数之间插入一个短划线和偶数之间的星号.每次输入都无法正常工作.它适用于例如46879,但是返回None为468799,或者不在4和6之间插入*4546793.为什么这样做?谢谢

def DashInsertII(num): 

 num_str = str(num)

 flag_even=False
 flag_odd=False

 new_str = ''
 for i in num_str:
  n = int(i)
  if n % 2 == 0:
   flag_even = True
  else:
   flag_even = False
  if n % 2 != 0:
   flag_odd = True
  else:
   flag_odd = False
  new_str = new_str + i
  ind = num_str.index(i)

  if ind < len(num_str) - 1:
   m = int(num_str[ind+1])
   if flag_even:
    if m % 2 == 0:
      new_str = new_str + '*'
   else:                 
    if m % 2 != …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
143
查看次数

标签 统计

python ×2

error-handling ×1

http ×1

python-requests ×1