【发布时间】:2020-11-21 22:05:47
【问题描述】:
import urllib.request
import time
import json
import random
QUERY = "http://localhost:8080/query?id={}"
N = 500
def getDataPoint(quote):
stock = quote['stock']
bid_price = float(quote['top_bid']['price'])
ask_price = float(quote['top_ask']['price'])
price = (bid_price + ask_price)/2
return stock, bid_price, ask_price, price
def getRatio(price_a, price_b):
if(price_b==0):
return
return price_a/price_b
if __name__ == "__main__":
for _ in range(N):
quotes = json.loads(urllib.request.urlopen(
QUERY.format(random.random())).read())
prices = {}
for quote in quotes:
stock, bid_price, ask_price, price = getDataPoint(quote)
prices[stock] = price
print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock,
bid_price, ask_price, price))
print ("Ratio %s" % getRatio(prices['ABC'], prices['DEF']))
Traceback(最近一次调用最后一次): 文件“C:/Users/AppData/Local/Programs/Python/Python37/client.py”,第 54 行,在 引号= json.loads(urllib.request.urlopen(QUERY.format(random.random())).read()) 文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 222 行,在 urlopen 返回 opener.open(url, 数据, 超时) 文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 531 行,打开 响应=方法(请求,响应) http_response 中的文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 641 行 'http', 请求, 响应, 代码, msg, hdrs) 文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 569 行,错误 结果 = self._call_chain(*args) _call_chain 中的文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 503 行 结果 = 函数(*args) 文件“C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py”,第 649 行,在 http_error_default 中 引发 HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError:HTTP 错误 404:未找到
我的 URL 出错了。进行了一些研究并试图弄清楚为什么客户端部分抛出错误而服务器部分工作正常。
【问题讨论】:
-
您是否在调试器中运行您的程序?它可能会引导您找到触发未定义行为的确切代码行。
-
您能帮忙解决上述问题吗?
-
你完全把这个问题从一个关于 C++ 的问题变成了一个关于 python 的问题。你为什么不问一个新问题?
标签: python python-3.x http http-status-code-404 urllib