我是Socket programming in python
的初学者。这是我尝试的第一个练习,但我得到403 forbidden error
。有人可以指出为什么会出现这个错误以及如何解决它?
>>> 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;">© 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))]
由于
答案 0 :(得分:1)
您错误地指定了HTTP GET请求。试试这个:
>>> msg="GET / HTTP/1.1\r\n"
请注意/
和HTTP
之间的空格。