0
import socket
host = 'www.google.com'
port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try :
 client.connect((host, port))
except socket.error:
 print ("Err")
package = '00101'
package.encode('utf-8')
client.sendall(package.encode(encoding = 'utf-8'))
response = client.recv(4096)
print (response.decode('UTF-8')

I kept getting b'' as my return, so I'm trying to decode it. The error I receive is unexpected EOF while parsing. Should I not include the decoding() function in my printing? I've tried printing only response, the .decode() function did not decode. What should I try?

Alastair McCormack
28k8 gold badges81 silver badges106 bronze badges
asked Jul 18, 2016 at 2:37
1
  • It looks like you have a SyntaxError due to an unclosed parenthesis on the last line... Commented Jul 18, 2016 at 2:39

1 Answer 1

1

You need to send a valid HTTP request. For example:

package = b'''GET /HTTP/1.1
Host: www.google.com
'''
client.sendall(package)

Which correctly returns a redirect on my machine. Note the empty line at the end of package, which ends the request.

When you send b'00101' and start reading, the google server has not yet processed your request and returns nothing. By sending a trailing newline (package = b'00101\n') it will start processing your request, and you will get:

...
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
answered Jul 18, 2016 at 2:57
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, didn't realize that specific string was that vital.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.