简体   繁体   中英

How to send entire http request from file?

I am writing a small application that interprets the http response of a request. I am writing the application in 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?

Check out the python requests library. 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.

r.status_code

Check out the library for more, it is very extensive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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