[Tutor] Python 3 simple socket issue

Dave Angel davea at davea.name
Wed Feb 18 18:06:37 CET 2015


On 02/18/2015 10:48 AM, Juan C. wrote:
> Code:
>
> # !/usr/bin/env python3
> # -*- coding: utf-8 -*-
>
> import socket
>
> def main():
> target_host = 'www.google.com'
> target_port = 80
>
> client = socket.socket()
> client.connect((target_host, target_port))
> client.send(b"GET HTTP/1.1\r\nHost:google.com\r\n\r\n")
>
> response = client.recv(4096)
> print(response)
>

When I run that, I get:

davea at think2b:~/zzztemp$ python juan.py
   File "juan.py", line 8
     target_host = 'www.google.com'
               ^
IndentationError: expected an indented block
davea at think2b:~/zzztemp$


So you might want to fix the indentation.  And add a call to main() at 
top level.

Once I do that, I get approximately the same thing you post below.

>
> Output:
>
> C:\Python34\python.exe D:/Documents/PyCharm/Test/__main__.py
> b'HTTP/1.0 403 Forbidden\r\nContent-Length: 1180\r\nContent-Type:
> text/html; charset=UTF-8\r\nDate: Wed, 18 Feb 2015 15:43:16 GMT\r\nServer:
> GFE/2.0\r\nAlternate-Protocol: 80:quic,p=0.08\r\n\r\n<html><head><meta
> http-equiv="content-type" content="text/html;
> charset=utf-8"/><title>Sorry...</title><style> body { font-family: verdana,
> arial, sans-serif; background-color: #fff; color: #000;
> }</style></head><body><div><table><tr><td><b><font face=times color=#0039b6
> size=10>G</font><font face=times color=#c41200 size=10>o</font><font
> face=times color=#f3c518 size=10>o</font><font face=times color=#0039b6
> size=10>g</font><font face=times color=#30a72f size=10>l</font><font
> face=times color=#c41200 size=10>e</font></b></td><td style="text-align:
> left; vertical-align: bottom; padding-bottom: 15px; width: 50%"><div
> style="border-bottom: 1px solid
> #dfdfdf;">Sorry...</div></td></tr></table></div><div style="margin-left:
> 4em;"><h1>We\'re sorry...</h1><p>... but your computer or network may be
> sending automated queries. To protect our users, we can\'t process your
> request right now.</p></div><div style="margin-left: 4em;">See <a href="
> https://support.google.com/websearch/answer/86640">Google Help</a> for more
> information.<br/><br/></div><div style="text-align: center; border-top: 1px
> solid #dfdfdf;"><a href="https://www.google.com">Google
> Home</a></div></body></html>'
>
> Process finished with exit code 0
>
> Why a I getting 403? The code seems fine.
>

Could it be because google.com doesn't want automated queries?  That's 
what they say in the text there.

See:
    http://www.google.com/intl/en/policies/terms/




-- 
DaveA


More information about the Tutor mailing list