发布请求后如何检索服务器响应

时间:2020-10-21 12:41:00

标签: python sockets request

发送发帖请求后,我如何知道服务器的响应?

import socket
import sys
import requests
  
try: 
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    print "Socket successfully created"
except socket.error as err: 
    print "socket creation failed with error %s" %(err) 
  
# default port for socket 
port = 80
  
try: 
    host_ip = socket.gethostbyname('fr2.website.com') 
except socket.gaierror: 
  
    # this means could not resolve the host 
    print "there was an error resolving the host"
    sys.exit() 
  
# connecting to the server 
s.connect((host_ip, port))

print "host ip %s connected"  %(host_ip)

headers = """\
POST /auth HTTP/1.1\r
Content-Type: {content_type}\r
Content-Length: {content_length}\r
Host: {host_ip}\r
Connection: close\r
\r\n"""

body = 'reloadToken=4bfc2a657de8b901fdb6990d4b9bf05a&setBid=true&ItemID=30&ItemType=Actionitem&currentSelecter=all&bidAmount=3&RTVT=main3d5b7b46e3f97a6d9b4a54da6d34&ajaxAction=biddOnItem'                                 
body_bytes = body.encode('ascii')
header_bytes = headers.format(
    content_type="application/x-www-form-urlencoded",
    content_length=len(body_bytes),
    host_ip=str(host_ip) + ":" + str(port)
).encode('iso-8859-1')

payload = header_bytes + body_bytes
s.sendall(payload)

0 个答案:

没有答案