0

I am trying to send a HTTP request to a web-server, I'm really new to this - started yesterday so I'm probs being stupid but everytime I send a request I only get a blank response, here is my python code:

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('192.168.0.40', 80))
msg = 'GET /testPy.html HTTP/1.0'
client.send(msg.encode())

response = client.recv(4096)
print(response.decode())

Don't know if it is something with my python or if it is something i need to set up on the webserver, many thanks in advance

my console:

Emilians-MacBook-Pro:python emil$ python tcp.py

Emilians-MacBook-Pro:python emil$ 

there is around a 10s delay until the newline

when i do not decode the request it prints

Emilians-MacBook-Pro:python emil$ python tcp.py
b''
Emilians-MacBook-Pro:python emil$ 
emilbowry
  • 11
  • 3
  • 2
    You can use [`urllib`](https://docs.python.org/3/library/urllib.request.html#module-urllib.request) or [`requests`](http://docs.python-requests.org/en/master/) to make HTTP requests in an easier way. – bfontaine Jun 18 '18 at 09:44
  • 1
    I know you've only a blank response, but can you print your console anyway please ? – AntoineLB Jun 18 '18 at 09:44
  • okay ive updated it @AntoineLB – emilbowry Jun 18 '18 at 09:57
  • Have a look at [this](https://stackoverflow.com/questions/31708519/request-returns-bytes-and-im-failing-to-decode-them). BTW, you should use `dir(your_object)` to get more information about it. – AntoineLB Jun 18 '18 at 10:02
  • the response i get when i dont decode is - b'' – emilbowry Jun 18 '18 at 10:12
  • You're sending a request without any header and without even a line break, I'm not sure how much the server would like it. – Marco Bonelli Jun 18 '18 at 10:15
  • so how am i meant to send a request @MarcoBonelli, thanks i needed to add a newline to my request – emilbowry Jun 18 '18 at 11:03
  • Well, take a look [here](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html). You'll need to add a CRLF at the end of your line to say the least. "*The Request-Line begins with a method token, followed by the Request-URI and the protocol version, and ending with CRLF. The elements are separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.*" – Marco Bonelli Jun 18 '18 at 11:06

0 Answers0