Reputation: 350
I am learning - how to send a request to the browser with twisted
then get the headers and print them. However, I find myself getting the following error when I run:
python agent_request.py http://www.google.com/
> cannot use a bytes pattern on a string-like object
import sys
from twisted.internet import reactor
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
def printHeaders(response):
print('HTTP version:', response.version)
print('Status code:', response.code)
print('Status phrase:', response.phrase)
print('Response headers:')
for header, value in response.headers.getAllRawHeaders():
print(header, value)
def printError(failure):
print(sys.stderr, failure)
def stop(result):
reactor.stop()
if len(sys.argv) != 2:
print(sys.stderr, "Usage: python print_metadata.py URL")
exit(1)
agent = Agent(reactor)
headers = Headers({'User-Agent': ['Twisted WebBot'],
'Content-Type': ['text/x-greeting']})
d = agent.request('HEAD', sys.argv[1], headers=headers)
d.addCallbacks(printHeaders, printError)
d.addBoth(stop)
reactor.run()
Expected output:
HTTP version: ('HTTP', 1, 1)
Status code: 200
Status phrase: OK
Response headers:
X-Xss-Protection ['1; mode=block']
Set-Cookie ['PREF=ID=b1401ec53122a4e5:FF=0:TM=1340750440...
Expires ['-1']
Server ['gws']
Cache-Control ['private, max-age=0']
P3p ['CP="This is not a P3P policy! See http://www.google.com/support/...
Content-Type ['text/html; charset=ISO-8859-1']
X-Frame-Options ['SAMEORIGIN']
Upvotes: 0
Views: 502
Reputation: 48335
On Python 3 sys.argv
is a list of str
. However, Agent.request
accepts a value of type bytes
as its 2nd argument. Since sys.argv[1]
is a value of type str
something goes wrong somewhere in the implementation and you get this obscure exception.
If you encode sys.argv[1]
to bytes (eg sys.argv[1].encode("ascii")
) and pass the result to agent.request
then you'll get past this error.
Upvotes: 2