python http OPTIONS請求

寒江雪語發表於2017-03-30

通過傳送http OPTIONS請求,可以獲取 http允許的方法(我這裡主要測試網站是否開啟webDav),測試如下:

import urllib2
import json
import httplib

url='192.168.149.131'
def http_get():
    conn = httplib.HTTPConnection(url)
    conn.request("GET", "/iisstart.htm")
    ret = conn.getresponse()
    response = ret.read() 
    return response

def http_options():
    conn = httplib.HTTPConnection(url)
    conn.request("OPTIONS", "/")
    ret = conn.getresponse()
    print ret.msg.dict
    print ret.msg.dict['allow']

ret = http_options()
#ret2 = http_get()
#print ret

結果如下:

E:\>python isWebDav.py
{'content-length': '0', 'accept-ranges': 'none', 'ms-author-via': 'DAV', 'server': 'Microsoft-IIS/6.0', 'dav': '1, 2', 'dasl': '<DAV:sql>', 'allow': 'OPTIONS, TRACE, GET, HEAD, COPY, PROPFIND, SEARCH, LOCK, UNLOCK', 'cache-control': 'private', 'date': 'Wed, 29 Mar 2017 15:59:20 GMT', 'public': 'OPTIONS, TRACE, GET, HEAD, DELETE, PUT, POST, COPY, MOVE, MKCOL, PROPFIND, PROPPATCH, LOCK, UNLOCK, SEARCH'}
OPTIONS, TRACE, GET, HEAD, COPY, PROPFIND, SEARCH, LOCK, UNLOCK


相關文章