问题
I have been trying for many days now, so here I am finally asking, may be dumb question for most of the experts.
I am using PyUnit for API testing of my application.
The application (to be tested) is deployed on one of the local servers over here. The application prevents hackers from doing malicious activities. So I am accessing any website (protected by this application) through this application. e.g. http://my-security-app/stackoverflow/login
, http://my-security-app/website-to-be-protected
etc.
Almost the whole framework is running around Urllib and Urllib2.
Scenario:- I need to send a request without 'Host' header or a request with 'Host123' as a header name instead of original, using python.
I have been trying below mentioned code. But its not working for me.
"Test failed because of an exception: [Errno 9] Bad file descriptor"
host = "12.1.3.16"
with closing(socket.create_connection((host, 80))) as s:
# send request without Host header
s.sendall(b"GET /mywebsite/administrator/ HTTP/1.1\r\n" +
#"Host12:{}\r\n".format(host).encode('ascii', 'strict') +
b"Connection: close\r\n\r\n")
Any help would be deeply appreciated.
Thanks in advance!
回答1:
Use httpbin test server for this. It is written in Python, btw.
Description:
Testing an HTTP Library can become difficult sometimes. PostBin.org is fantastic for testing POST requests, but not much else. This exists to cover all kinds of HTTP scenarios. Additional endpoints are being considered (e.g. /deflate).
All endpoint responses are JSON-encoded.
Author provides even public service based on this server at http://httpbin.org
来源:https://stackoverflow.com/questions/22375673/how-to-send-a-request-without-host-header-using-python