简体   繁体   中英

How to store multiple values in keys in dictionary in python?

Maybe my question title is different from the question content.

statuscode = []
statuscode.append(200) 

for x in find_from_sublister(hostname):
    x2 = x.strip()
    url = "http://"+ x2
    try:
        req = requests.get(url)
        req1 = str(req.status_code) + " " + str(url) + '\n'
        req2 = str(req.status_code)
        req3 = str(url)
        dict = {req2 : req3}

        print " \n " + str(req1)

    except requests.exceptions.RequestException as e:
        print "Can't make the request to this Subdomain " + str(url) + '\n'

for keys, values in dict.iteritems():
    print "finding the url's whose status code is 200"

    if keys == statuscode[0]:
        print values
    else:
        print "po"

I am using the code to do some following stuffs.

  1. It will find the subdomains from the Sub-lister (Locally)
  2. Then it will go to find the status code of each subdomain which was find by the sublister, with the help of for loop . for x in find_from_sublister(hostname):

Note: find_from_sublister(hostname) is a function for finding the subdomain from the sublister.

  1. Then print the status code with the URL. Here print " \\n " + str(req1)

[All goes well, but the problem starts here ]

Now what I want is to seperate the URLs which have 200 status code.

so I heard then it can happen by using the dictionary in python. So, I try to use the dictionary. As you can see dict = {req2 : req3}

Now I also make a list of index which have value 200

Then I compare the keys to list of index. here keys == statuscode[0]

and if they match then it should print all the URL's which have 200 status code.

But the result I am getting is below,

finding the url's whose status code is 200 
po 

You see the value po its else statment value,

else:
   print "po"

Now the problem is Why I am getting this else value why not the URL's which have status code 200?

Hope I explained you clearly. And waiting for someone who talk to me on this.

Thanks

Note: I am Using Python 2.7 ver.

In your case, you don't even need the dictionary.

I've tried to clean up the code as much as possible, including more descriptive variable names (also, it's a bad idea to override builtin names like dict ).

urls_returning_200 = []

for url in find_from_sublister(hostname):
    url = "http://" + url.strip()
    try:
        response = requests.get(url)
        if response.status_code == 200:
            urls_returning_200.append(url)
        print " \n {} {}\n".format(response.status_code, url)
    except requests.exceptions.RequestException as e:
        print "Can't make the request to this Subdomain {}\n".format(url)

print "finding the url's whose status code is 200"
print urls_returning_200

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