[Tutor] Python 3 simple socket issue

Juan C. juan0christian at gmail.com
Wed Feb 18 16:48:14 CET 2015


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)


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.

Python 3.4.2


More information about the Tutor mailing list