0

When I execute this script, I always get a Bad Request output. I don't understand what is going wrong. I wanted to print the output of my cmd variable and I see a b' as the prefix. I think this is how it is going to the server as a request and as a result it's failing. It's Python 3.7.0, by the way.

My code snippet is as follows:

import socket

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

while True:
    data = brows.recv(512)
    if (len(data) < 1):
        break
    print(data.decode())
brows.close()

My output is as follows:

enter image description here

I'm completely new to Python. Can someone please help me solve this?

LearneR
  • 2,351
  • 3
  • 26
  • 50
  • 1
    I don't think the b is a problem, by itself. A "b" prefix indicates that the object is a bytes object. That's more or less the de-facto standard type for transmitting data between machines/processes/whatever. The [socket.send](https://docs.python.org/3.7/library/socket.html#socket.socket.send) documentation confirms that its first argument should be bytes, so you're good on that front. If you're thinking "ok, the type is fine, but what about the content? Isn't it bad that the first character is 'b'?", don't worry. The b isn't actually part of the data. The first character of your bytes is "G". – Kevin Nov 16 '18 at 18:31
  • Are you doing this to learn about sockets, or are you actually trying to request data from that server? Because if the latter, there are much easier ways to do it. – Daniel Roseman Nov 16 '18 at 18:35
  • I'm doing this as a part of Python for Everybody course. https://www.youtube.com/watch?v=Lr9Vm-VghAk&list=PLlRFEj9H3Oj7Bp8-DfGpfAfDBiblRfl5p&index=51&t=0s – LearneR Nov 16 '18 at 19:46

1 Answers1

0

I don't think the b' is the problem. That's just how bytes objects (which is what .encode() returns) are printed or converted to strings with str().

I don't know what your server is like, but I would guess that the problem is in the request, not the Python.

I would make one other recommendation, however. Use with to make sure the socket closes automatically at the end, whether there is a failure or not. As it is, if the program has an exception, the socket will not be closed. The with syntax tells Python to try to run it and automatically close it when done.

with socket.socket(...) as brows:
    # socket is open; we can do stuff with it
    brows.connect(...
# The socket is no longer open here

You don't even need to call .close() explicitly at the end!

Edit: To clarify, the socket is only open within the with block.

dtauxe
  • 153
  • 11
  • I changed the code as suggested and now it's having an issue with `data = brows.recv(512)` It saying: `OSError: [WinError 10038] An operation was attempted on something that is not a socket` - any idea what is going wrong? – LearneR Nov 16 '18 at 19:50
  • Based on [this answer](https://stackoverflow.com/questions/15210178/python-socket-programming-oserror-winerror-10038-an-operation-was-attempted-o#15210251), I'd guess it's indentation. I should clarify that the socket is closed at the end of the with block. So everything you want to do with the socket (Which is the rest of your program in this case) needs to be indented. – dtauxe Nov 16 '18 at 20:28
  • Thanks for the clarification. I indented as suggested, and now it just went back to the initial error (which is better than the failure). `HTTP/1.1 400 Bad Request` - I'm back to square one :( – LearneR Nov 16 '18 at 21:00
  • OKAY. FINALLY... it worked after changing to `cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()` - I have no idea why `\r\n\r\n` made such a world of difference in that line instead of `\n\n` - Can anyone plz explain? – LearneR Nov 16 '18 at 21:27
  • Understood from [this answer](https://stackoverflow.com/a/14606871/3234994) that `the carriage return-newline pair is both needed for newline in a network virtual terminal session.` - thank you all very much for taking time to help me :) – LearneR Nov 16 '18 at 21:39