python socket GET

前端 未结 6 1414
醉酒成梦
醉酒成梦 2020-12-09 20:20

From the other posts on stack overflow this should be working

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                 

s.connec         


        
6条回答
  •  伪装坚强ぢ
    2020-12-09 20:57

    Your code is almost right, but you need to send 2 \r\n sequences to satisfy the HTTP protocol.

    A valid GET request will look like this (note 2 lines):

    GET / HTTP/1.1
    
    

    So your code should be:

    s.sendall('GET / HTTP/1.1\r\n\r\n')
    

    Further to that, there are additional headers required for valid HTTP 1.1 requests, such as Host:. You need to add them to your request, something like this:

    s.sendall('''GET / HTTP/1.1
    Host: cnn.com
    
    ''')
    

提交回复
热议问题