简体   繁体   中英

how to send a plain http request to a url?

I have an HTTP request generated with postman:

GET http://google.com/ HTTP/1.1
cache-control: no-cache
User-Agent: PostmanRuntime/7.3.0
Accept: */*
Host: google.com
accept-encoding: gzip, deflate
Connection: keep-alive

I'm receiving these types of HTTP requests from a client via socket in python and I want to send it to its specified URL.

I'm familiar with requests library but considering the fact that the client may send any type of HTTP request, it seems inefficient to break down the request and rebuild it.

is there any easy and convenient way to send this request directly to its URL?

thank you in advance.

apparently one way that we can achieve this is by using TCP socket as mentioned in the comments, it can be done like this:

server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
port = 80
try:
    server_socket.connect((url,port))
    server_socket.send(str.encode(body)) # encoding needed for streaming in bytes
except socket.error as e:
    print(str(e))
    sys.exit(1)

as a side note, url must not include 'http://' or 'https://'. if so it raises an error

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