如何使用Python发送不带“主机头"的请求? [英] How to send a request without 'Host Header' using Python?

查看:64
本文介绍了如何使用Python发送不带“主机头"的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多天,所以我终于要问了,对于大多数专家来说,这可能是一个愚蠢的问题.
我正在使用PyUnit进行应用程序的API测试.该应用程序(待测试)部署在此处的一台本地服务器上.该应用程序可防止黑客进行恶意活动.因此,我正在通过此应用程序访问任何受此应用程序保护的网站.例如 http://my-security-app/stackoverflow/login http://my-security-app/website-to-be-protected 等.

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.

几乎整个框架都围绕Urllib和Urllib2运行.

Almost the whole framework is running around Urllib and Urllib2.

场景:-我需要使用python发送不带"Host"标头的请求,或发送带有"Host123"作为标头名称的请求,而不是原始的请求.

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.

由于异常,测试失败:[Errno 9]错误的文件描述符"

    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")   

任何帮助将不胜感激.

提前谢谢!

推荐答案

为此使用 httpbin 测试服务器.它是用Python写的,顺便说一句.

Use httpbin test server for this. It is written in Python, btw.

说明:

测试HTTP库有时会变得很困难.PostBin.org是非常适合测试POST请求,但其他的就不多了.这存在于涵盖各种HTTP方案.其他端点正在考虑(例如/deflate).

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).

所有端点响应都是JSON编码的.

All endpoint responses are JSON-encoded.

作者甚至在 http://httpbin.org

这篇关于如何使用Python发送不带“主机头"的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆