-4

What does ":" and "+2" mean in [:http_payload.index("\r\n\r\n")+2]? What does "("/")" and "[1]" mean in .split("/")[1]?

def get_http_headers(http_payload):
    try:
        # split the headers off if it is HTTP traffic
        headers_raw = http_payload[:http_payload.index("\r\n\r\n")+2]

        # break out the headers
        headers = dict(re.findall(r"(?P<name>.*?): (? P<value>.*?)\r\n", headers_raw))

    except:
        return None

    return headers

def extract_image(headers, http_payload):
    image = None
    image_type = None

    try:
        if "image" in headers["Content-Type"]:
            # grab the image type and image body
            image_type = headers["Content-Type"].split("/")[1]

            image = http_payload[http_payload.index("\r\n\r\n")+4:]



            except:
                pass
    except:
        return None, None

    return image, image_type
john
  • 3
  • 2
  • There are 3 distinct questions here (or 5, depending on how you count). This is not really the format expected here, which is a specific problem you have, with some proof you have attempted to solve it. – Samuel Dion-Girardeau Mar 31 '19 at 16:14
  • @SamuelDion-Girardeau question updated. An answer would be highly appreciated! Thank you! – john Mar 31 '19 at 18:29
  • Duplicate of https://stackoverflow.com/questions/509211/understanding-slice-notation and https://stackoverflow.com/questions/743806/how-to-split-a-string-into-a-list and https://stackoverflow.com/questions/15456845/getting-a-list-item-by-index. – John Kugelman Mar 31 '19 at 18:33
  • Look for slicing in this page. https://docs.python.org/3/tutorial/introduction.html or https://www.datadependence.com/2016/05/python-sequence-slicing-guide/ – dr jimbob Mar 31 '19 at 18:34

1 Answers1

1

http_payload[:http_payload.index("\r\n\r\n")+2] slices the string http_payload so that only the head of the string up to the first appearance of "\r\n\r\n" and the first "\r\n" remains. the .index() method of a string will return the index of the first appearance of the pattern in the string.

Example:

test = "abcdefg"
# slicing:
print(test[1:3])  # will output 'bc'

# index:
print(test.index('bc'))  # will output 1 (index of start of substring 'bc')

# either start or end (or both) of the slice can be left out, so the following is equivalent:
print(test[:2] == test[0:2])  # will output True

.split("/")[1] will split a string at "/" characters and return a list, from which the item with index 1 is accessed. See the following code for example:

test = "/this/is/a/path"
print(test.split("/"))  # will output ["this", "is", "a", "path"]
print(test.split("/")[0])  # will output "is" since element of index 1 of the resulting list is accessed.
dudenr33
  • 1,119
  • 1
  • 10
  • 26
  • Thanks a lot! Could you please give a simple example for each? And what is the meaning of ":" and "+2" in "[:http_payload.index("\r\n\r\n")+2]"? Thanks a lot for you help! – john Mar 31 '19 at 21:27
  • Help would be highly appreciated! @dudenr33 – john Apr 01 '19 at 00:15
  • Please read the link provided. The ":" separates the start and end index of the slice. `http_payload.index("\r\n\r\n")` will return an integer, to which the "+2" gets added. So if this evaluates to 10 (for example), it would be equivalent to `http_payload[:12]`, which is short for `http_payload[0:12]`. This basically means "take the characters 0 to 12 from string `http_payload`". Please work through a Python tutorial of your choice and refer to the Python docs before asking questions on StackOverflow. StackOverflow is neither a replacement for the former nor for the latter. – dudenr33 Apr 01 '19 at 16:18