Error sending - get request.

Asked

Viewed 100 times

1

Considering the code below:

path = "/admin/"
host = "192.168.1.1"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))

req = ("GET /"+path+" HTTP/1.1\n Host: %s \r\n\r\n", host)

s.send(req)
print(s.recv())
s.close()

I’m trying to send a simple request get but I’m not getting it. By concatenating tupla with string get the following error message:

Typeerror: must be string or path, not tuple

What’s wrong with this code?

  • What was your intention to put , host along those lines? I don’t understand the intention. req = ("GET /"+path+" HTTP/1.1\n Host: %s \r\n\r\n", host)

  • my intention to add the var host to my %string

  • That’s not how it works. You probably imagined something along those lines: req = "GET /"+path+" HTTP/1.1\n Host: "+host+" \r\n\r\n" - in which way, the answer given should solve the problem.

  • then I went around confusing everything, and that my keyboard is defective so I copied this excerpt of code from the net but I had already studied it

1 answer

1


The error must happen on the line s.send(req) - the socket.send method expects a string (or a buffer, not a "path" - will be that you transcribed the message incorrectly?).

What happens is that on the line you create the content of req you are not formatting the string, but creating a "tuple" - a sequence of two elements separated by "," - the formatting of strings using "%" uses the "%" operator between the string and the parameters, not a function call (or something like that - which is what you do there) - where the parameters are separated by ",".

That is, re-write your line that defines req to make it:

req = "GET /%s HTTP/1.1\n Host: %s \r\n\r\n" % (path, host)

instead of the way it is. The syntax of formatting strings with "%" has been deprecated in recent years by the new way of formatting strings with the "format" method: it is typed a little more, but it is a little less manageable to understand and read - and has more flexibility in advanced cases - in this case, your line would look like this (and you would not have confused yourself with the syntax as it did):

req = "GET /{} HTTP/1.1\n Host: {} \r\n\r\n".format(path, host)

Note that in both cases, as well as the "host""path" can also be passed as a formatting parameter - the possibility of a very flexible and feature-rich string formatting is one of the strengths of Python - and there are never reasons to use string concatenation with "+" just to interpolate values, as you did with the path - it is worth taking a look at the documentation of format: https://docs.python.org/2/library/string.html#format-string-syntax

Another tip: in addition to the error message, put also an indication of the line where the error occurred - the interpreeser tells you which one was.

  • now came out cute but once thank you was just that

  • and in the recv(#) # function the amount of bytes you want to recover should be placed and not sent to the file eg recv(1024)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.