0

I'm having some issues with this python client. I'd appreciate it if someone could tell me what's wrong.

import socket, sys, time, os

host = '155.94.243.10'
port = 80

mySocket = socket.socket()
mySocket.connect((host,port))

message = input('>>>')
while message != 'q':
    mySocket.send(message.encode())
    data = mySocket.recv(1024).decode()
    print('Received from server: ' + str(data))
    message = input('>>>')
mySocket.close()

I'm using "GET / HTTP/1.1" as the input.

I get no response from the server, I should be getting an error message (I think)

Edit: I used wireshark to confirm I am connecting to the server.

Thanks in advance.

7
  • 1
    I suggest that you stop reinventing the wheel and use one of the many python http clients out there. python-requests can be highly recomended Commented Nov 12, 2016 at 3:47
  • 1
    @e4c5 I'll make sure to take a look at it! I'm also doing this just to learn about TCP though. Thank you for the suggestion. Commented Nov 12, 2016 at 3:52
  • @Zimm3r No, this is very clearly an HTTP client connecting to an HTTP server, not itself. It sends, the HTTP server reads the input, then sends back a response, which the program reads. It is structured correctly. Commented Nov 12, 2016 at 3:56
  • @Zimm3r Thank you for your suggestion. I tried it but still got the same results. Commented Nov 12, 2016 at 4:04
  • Learn HTTP. Client has to send empty line after all headers. So you have to send "GET / HTTP/1.1\n\n". Now serwer is waiting for new line. Commented Nov 12, 2016 at 4:04

1 Answer 1

2

Client has to send empty line aftera all headers. It inform server that it get all headers and it can send response (or it has to read body if you send POST).

import socket
import sys
import time
import os

#host = '155.94.243.10'
host = 'stackoverflow.com'
port = 80

mySocket = socket.socket()
mySocket.connect((host,port))

message = input('>>>')

while message != 'q':

    message += '\n\n'
    #message = 'GET / HTTP/1.1\n\n'

    mySocket.send(message.encode())
    data = mySocket.recv(1024).decode()
    print('Received from server: ' + str(data))
    message = input('>>>')
mySocket.close()

EDIT: It seems '155.94.243.10' needs other headers to get result. Try

message = 'GET / HTTP/1.1\nHost: 155.94.243.10\n\n'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was trying to add the newlines in with the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.