0

I am new at Python and I have been trying to figure out the following exercise.

Exercise 5: (Advanced) Change the socket program so that it only shows data after the headers and a blank line have been received. Remember that recv is receiving characters (newlines and all), not lines.

I attached below the code I came up with, unfortunately I don't think it is working:

import socket
mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
mysocket.connect(('data.pr4e.org', 80))
mysocket.send('GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode())

count=0
while True:
          data = mysocket.recv(200)

          if (len(data) < 1): break  

          count=count+len(data.decode().strip())
          print(len(data),count)
          if count >=399:
                 print(data.decode(),end="")         
mysocket.close()

2 Answers 2

1

Instead of counting the number of lines received, just grab all the data you get and then split on the first double CRLF you find.

resp = []
while True:
          data = mysocket.recv(200)

          if not data: break  
          resp.append(data.decode())
mysocket.close()

resp = "".join(resp)
body = resp.partition('\r\n\r\n')[2]
print(body)
Sign up to request clarification or add additional context in comments.

2 Comments

I will study the partition as I have always focused on split.
@PythonLearner1977 you can use resp.split('\r\n\r\n', 1)[1] (the argument 1 to split tells it to perform only one split) to get the same result here. I just have a liking for partition, no other reason for using it specifically.
0
import socket


user_url = input('Enter a url: ')
if len(user_url) < 1:
    user_url = 'http://data.pr4e.org/romeo.txt'
try:
    host_name = user_url.split('/')
    host_name = host_name[2]

    # create the socket
    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # make a connection to the host server
    my_socket.connect((host_name, 80))

    # create http GET request, encode it, then send it
    cmd = ('GET ' + user_url + ' HTTP/1.0\r\n\r\n').encode()
    my_socket.send(cmd)

    # create an empty string
    my_str = ''
    while True:
        # receive the data 512 byte characters at a time
        data = my_socket.recv(512).decode()
        # append the decoded data to the empty string
        my_str += data

        # after we receive all data, exit the loop
        if len(data) == 0:
            break
            
    my_socket.close()
    # split my string into 2 parts based on: \r\n\r\n
    my_str = my_str.split('\r\n\r\n')
    # the result is the second part of the string
    print(my_str[1])

except:
    print('Invalid URL')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.