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.
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)
– Bacco
my intention to add the var host to my %string
– user45474
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.– Bacco
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
– user45474