How to send a request without 'Host Header' using Python?

大兔子大兔子 提交于 2019-12-11 09:22:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!