-2

I am making a small project. The project is in the python and I am now stuck in a problem and i.e.

I have a list of URLs with their response code like 200, 300, 400, 403.

Okay let's make it more clear

http://aa.domain.com 200
http://bb.domain.com 302
http://cc.domain.com 400
http://dd.domain.com 403
http://ee.domain.com 403

Now what I exactly want is I want to seperate the URLs with their status code.

Like a "Making a Seperate list of 400 and 403 URLs".

How can I do it with python? As a newbie, I can't. Could You?

EDIT:- I have tried this

        try:
         req = requests.get(xxx) #xxx is a list of subdomains
         responsecode = xxx , req.status_code, "\n"

         print responsecode

        except requests.exceptions.RequestException as e:   
            print "Not full fill request"

I only can print the response code. And successfully print the response code as like I said above

  • 4
    can you show expected output? also, what have you tried so far – usernamenotfound Feb 12 '18 at 04:26
  • Well if you are a newbie let us know what you tried before asking for help. – anurag0510 Feb 12 '18 at 04:29
  • Do you want to make separate lists for URLs with different responses? Why not maintain a dictionary which looks as follows: `{ 400: [list,of,url], 200: [another,list,..], etc.. }` – Sudheesh Singanamalla Feb 12 '18 at 04:31
  • You should do what @SudheeshSinganamalla suggested. And use this to do that: [Python dict how to create key or append an element to key?](https://stackoverflow.com/questions/12905999/python-dict-how-to-create-key-or-append-an-element-to-key). – Keyur Potdar Feb 12 '18 at 04:42

2 Answers2

0

You can use split function to split the request in to two (request code & URL) & inserted into dictionary till the size of request list .

I am also attach the screenshot of the output .

dictionary = {}
request = ["http://aa.domain.com 200", "http://bb.domain.com 302", "http://cc.domain.com 400", "http://dd.domain.com 403", "http://ee.domain.com 403"]
for i in range(0, len(request)) :
  word = request[i].split(" ")[1];
  dictionary[word] = request[i].split(" ")[0];


print(dictionary)

enter image description here

Usman
  • 1,983
  • 15
  • 28
-1

Well for the query you asked we can solve it by using dictionary in python for desired output. Checkout the code snippet below :

dict = {}
req = ["http://aa.domain.com 200", "http://bb.domain.com 302", "http://cc.domain.com 400", "http://dd.domain.com 403", "http://ee.domain.com 403"]
for i in range(0, len(req)) :
    term = req[i].split(" ")
    while True :
        if term[1] not in dict.keys():
            dict.setdefault(term[1], [])
            dict[term[1]].append(term[0])
            break
        else :
            dict[term[1]].append(term[0])
            break
print(dict)

It gives the desired output as :

{'200': ['http://aa.domain.com'], '400': ['http://cc.domain.com'], '403': ['http://dd.domain.com', 'http://ee.domain.com'], '302': ['http://bb.domain.com']}

Let me know if you are unable to understand it.

anurag0510
  • 763
  • 1
  • 8
  • 17