Self admitted PHP coder turning to the Python side and my head hurts. I'm trying to do what I think would be simple. Read in a file (have that working) and then store each lines input into a variable then evaluate the string variable into an existing string of text.
Here's what I have:
with open('./users.txt') as users:
for user in users:
conn.request("GET", "/vmrest/users?query=(alias%2520is%2520{})".format(user), headers)
res = conn.getresponse()
data = res.read()
All I want is the value in my user variable to be placed right at the end of the string right after "/vmrest/users?query=(alias%2520is%2520user variable here", headers)
Thanks
EDIT Realized I didn't include what was happening sorry. Below is the feedback I get when executing.
Traceback (most recent call last):
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 885, in send
self.sock.sendall(data)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/ssl.py", line 886, in sendall
v = self.send(data[count:])
TypeError: unhashable type: 'slice'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "amer-unity.py", line 12, in <module>
conn.request("GET", "/vmrest/users?query=(alias%2520is%2520{})".format(user), headers)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 1079, in endheaders
self._send_output(message_body)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 913, in _send_output
self.send(message_body)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/http/client.py", line 889, in send
self.sock.sendall(d)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/ssl.py", line 886, in sendall
v = self.send(data[count:])
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/ssl.py", line 856, in send
return self._sslobj.write(data)
File "/opt/rh/rh-python35/root/usr/lib64/python3.5/ssl.py", line 581, in write
return self._sslobj.write(data)
TypeError: a bytes-like object is required, not 'str'
requestsfor sending web requests. It's much better than the Python standard lib.)user.strip())requestmethod is expecting abytesinstead ofstr, but that's a big guess on my part. Astris a text value, and unlike in Python 2.x, it can't be used as a byte sequence. I really recommend switching torequestsand seeing if it just works; my suspicion is that it will sort of magically eliminate these kinds of problems.