简体   繁体   English

如何从文件发送整个http请求?

[英]How to send entire http request from file?

I am writing a small application that interprets the http response of a request.我正在编写一个解释请求的 http 响应的小应用程序。 I am writing the application in python.我正在用 python 编写应用程序。 I have not found anything that allows me to send the body + headers stored in one file.我没有找到任何允许我发送存储在一个文件中的正文 + 标题的内容。 I can send certain parts like the headers but not the entire request.我可以发送某些部分,如标头,但不能发送整个请求。 For example, if the request is:例如,如果请求是:

GET /index.html HTTP/1.1
Host: localhost
Cookie: bob=lemon

I want to send this entire request in one go.我想一次性发送整个请求。 How would I do this in python?我将如何在 python 中做到这一点?

Check out the python requests library.查看 python 请求库。 https://requests.readthedocs.io/en/master/user/quickstart/#make-a-request https://requests.readthedocs.io/en/master/user/quickstart/#make-a-request

For the request above it would look something like对于上面的请求,它看起来像

import requests 

url = 'http://localhost:[YOUR PORT HERE]/'
cookies = {bob : lemon}

r = requests.get(url, cookies=cookies)

To check if you had a successful request you should get a 200 code from.要检查您的请求是否成功,您应该从中获取 200 代码。

r.status_code

Check out the library for more, it is very extensive.查看图书馆了解更多信息,它非常广泛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM