2014-01-08 34 views
0

我是Socket programming in python的初學者。這是我嘗試的第一個練習,但是我得到403 forbidden error。有人可以指出爲什麼會出現這個錯誤,以及如何解決它?當我使用python中的套接字從Google獲取請求時發生錯誤?

>>> streams = [info for info in socket.getaddrinfo('google.com','http') if info[1]==socket.SOCK_STREAM] 
>>> info=streams[0] 
>>> google_socket=socket.socket(*info[:3]) 
>>> google_socket.connect(info[-1]) 
>>> msg="GET /HTTP/1.1\r\n" 
>>> msg 
'GET /HTTP/1.1\r\n' 
>>> msg+="Host: google.com\r\n\r\n" 
>>> google_socket.sendall(msg) 
>>> buffsize=4096 
>>> response="" 
>>> done=False 
>>> while not done: 
... msg_part=google_socket.recv(buffsize) 
... if len(msg_part)<buffsize: 
...  done=True 
...  google_socket.close() 
... response+=msg_part 
... 
>>> len(response) 
1372 
>>> response 
'HTTP/1.0 403 Forbidden\r\nContent-Length: 1201\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Wed, 08 Jan 2014 03:39:14 GMT\r\nServer: GFE/2.0\r\nAlternate-Protocol: 80:quic\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;">&copy; 2013 Google - <a href="https://www.google.com">Google Home</a></div></body></html>' 
>>> streams 
[(2, 1, 6, '', ('173.194.33.33', 80)), (2, 1, 6, '', ('173.194.33.41', 80)), (2, 1, 6, '', ('173.194.33.32', 80)), (2, 1, 6, '', ('173.194.33.34', 80)), (2, 1, 6, '', ('173.194.33.35', 80)), (2, 1, 6, '', ('173.194.33.36', 80)), (2, 1, 6, '', ('173.194.33.37', 80)), (2, 1, 6, '', ('173.194.33.38', 80)), (2, 1, 6, '', ('173.194.33.39', 80)), (2, 1, 6, '', ('173.194.33.40', 80)), (2, 1, 6, '', ('173.194.33.46', 80)), (30, 1, 6, '', ('2607:f8b0:400a:804::1002', 80, 0, 0))] 

感謝

+5

錯誤似乎給你打電話'...但你的電腦或網絡可能正在發送自動查詢......' – AlG

+1

這是一個403錯誤不是404 – Pruthvikar

+0

該錯誤還有[指向Google幫助的鏈接]( https://support.google.com/websearch/answer/86640)在錯誤消息中。你嘗試閱讀嗎? – abarnert

回答

1

您錯誤地指定了HTTP GET請求。試試這個:

>>> msg="GET/HTTP/1.1\r\n" 

/HTTP之間的空間。

相關問題