searching for string in returned header

34 views Asked by At

I'm having difficulty in searching a returned header for certain information. When I print the result from s.recv it shows the full header. I tried to search line by line for the word 'Server' but doing this displayed one character per line. I don't want to use regex for this task.

#!/usr/bin/python

import ipaddress, socket

port = 80

net = ipaddress.ip_network('216.58.208.78/32')
for x in net:
    x = str(x)
    s = socket.socket()
    s.connect((x,port))
    print "Connected to port"
    s.send("GET\r\n")
    print "Returned header..\n"
    header = s.recv(1024)
    for line in header:
         if 'Server' in line:
              print line
    s.close()
1

There are 1 answers

0
Eric Appelt On BEST ANSWER

Calling recv on a socket returns a string. If you iterate over a string as you do in for line in header: you will loop over each character in the string. To iterate over lines do:

for line in header.splitlines():