Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 396
Arya11 Avatar asked Dec 31 '25 22:12

Arya11


1 Answers

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. This was the main reason that my code was not working initially.

like image 54
Arya11 Avatar answered Jan 03 '26 10:01

Arya11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!