简体   繁体   中英

urllib.error.HTTPError: HTTP Error 404: Not found - python

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 (most recent call last): File "C:/Users/AppData/Local/Programs/Python/Python37/ client.py", line 54, in quotes= json.loads(urllib.request.urlopen(QUERY.format(random.random())).read()) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error result = self._call_chain(*args) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in in http_error_de fault raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found

I've error with the URL.Did some research and tried to clear still not so sure why the client part throws error while server part works fine.

This is caused due to the firewall of your computer blocking port 8080. Change the port from 8080 to say 8085 in both the client and server files.

In the above code, Change QUERY = "http://localhost:8080/query?id={}" To QUERY = "http://localhost:8085/query?id={}"

Similarly, there should be 8080 as the port number in the server file, change it to 8085.

Another solution would be to disable your firewall, which is not recommended.

Simply say, your server application is not running in the said location: http://localhost:8080/query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM