有没有办法使用套接字发送/审查网站数据?

时间:2018-05-03 22:24:00

标签: python sockets

是否有办法使用套接字从Google搜索等网站发送/审核数据。使它运行一个程序,它搜索一些固定值,然后在Python shell中输出结果。

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = 'google.com'
port = 80
server_ip = socket.gethostbyname(server)
s.connect((server, port))

1 个答案:

答案 0 :(得分:0)

是。尝试发送HTTP GET请求,例如对HTTP 1.1服务器的最低要求:

import socket

s = socket.socket()
s.connect(('httpbin.org', 80))

request = '\r\n'.join(('GET /get HTTP/1.1', 'Host: httpbin.org', '', ''))
s.send(request)
response = s.recv(1024)

>>> print(response)
HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.7.1
Date: Thu, 03 May 2018 22:40:59 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0
Content-Length: 159
Via: 1.1 vegur

{
  "args": {}, 
  "headers": {
    "Connection": "close", 
    "Host": "httpbin.org"
  }, 
  "origin": "220.233.14.203", 
  "url": "http://httpbin.org/get"
}

然而,这比你需要做的工作多得多。考虑使用requests

等库
import requests
r = requests.get('http://httpbin.org/get')
>>> print(r.text)
{
  "args": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4"
  }, 
  "origin": "220.233.14.203", 
  "url": "http://httpbin.org/get"
}

或标准库urlopen()功能。