我需要python 2.7的帮助,需要使用命令“ urllib2.Request”从本地Web服务器读取数据 只要服务器是可访问的(192.168.5.44),我就使用代码(在下面附上)并且可以正常工作。如果服务器不可用,则代码会停止并显示错误
public class MessageQuery : ObjectGraphType<Message>
{
public MessageQuery()
{
Field(o => o.Content).Resolve(o => "This is Content").AuthorizeWith("Authorized");
Field(o => o.SentAt);
Field(o => o.Sub).Resolve(o => "This is Sub");
}
}
我应该在代码中添加些什么,以便在发生错误的情况下继续运行并增加read的值(例如N / A或其他内容。....) 我有几个这样的服务器,如果一台服务器宕机,一切都会停止。
我的代码:
select po.Product from products po inner join
(
select pr.product,pr.Period,pr.Price from products pr inner join
(
select product,max(period)as [period] from products
group by product
) mx on mx.Product = pr.Product and mx.period = pr.Period
) mxx on mxx.Product = po.Product
and po.Period = mxx.Period-1
and mxx.Price <> po.Price
答案 0 :(得分:0)
使用try
和except
块来避免由于任何错误而停止程序,如下所示:
try:
request = urllib2.Request("http://192.168.5.44")
fip44 = urllib2.urlopen(request)
time.sleep(1)
sip44 = fip44.read()
def beri44( sip44, first, last ):
start = sip44.index( first ) + len( first )
end = sip44.index( last, start )
return sip44[start:end]
jed = float (beri44( sip44, "[", "]" ))
fjed = open("/var/www/html/Temp/jed.txt", "w")
fjed.write(str(jed))
fjed.close()
print"jed:", jed
except:
# Do whatever you want here in the case of an error occurred
pass
希望有帮助。