python http head 工具

群上闲聊,说道自己封装了一个 python 的 http head 工具

#!/usr/bin/env python

from socket import *

while True:
    HOST = raw_input('> ')
    if not HOST:
        break
    tcpCliSock = socket(AF_INET, SOCK_STREAM)
    PORT = 80
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    tcpCliSock.connect(ADDR)
    data = "HEAD / HTTP/1.1rnHost: %srnrn" % HOST
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZ)
    if not data:
        break
    print data
tcpCliSock.close()

输入输出

C:UsersZRJDesktop>python head
> 360.cn
HTTP/1.1 200 OK
Date: Thu, 18 Oct 2012 12:56:35 GMT
Server: Apache
P3P: CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi S
AMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE G
OV"
Set-Cookie: B=ID=809401350564995:V=2:S=236f7e7338; path=/; expires=Sun, 17-Jun-2
038 23:59:59 GMT; domain=.360.cn
Set-Cookie: UT=T=1350564995:F=MzYwLmNu:S=1; path=/; domain=.360.cn
Vary: Accept-Encoding
Connection: close
Content-Type: text/html

Leave a Reply

Your email address will not be published. Required fields are marked *