向服务器发出请求,如下面的代码所示,我有状态码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) 使用此代码,我想要做的就是在偶数之间插入一个短划线和偶数之间的星号.每次输入都无法正常工作.它适用于例如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)