python http应用

1、使用代理
>>> import urllib.request
>>> proxies = {'http': ' http://proxy.example.com:8080/'}
>>> opener = urllib.request.FancyURLopener(proxies)
>>> opener.addhead("Accept", "sound/basic")
>>> f = opener.open(" http://www.python.org")
>>> f.read().decode('utf-8')

2、GET请求

import urllib.request
f = urllib.request.urlopen(' http://www.pcb.org/pcb?a=b&c=d')
dir(f)

import urllib.request
req = urllib.request.Request(' http://www.example.com/')
req.add_header('Referer', ' http://www.python.org/')
r = urllib.request.urlopen(req)
对于GET请求,已经完备了

3、POST请求
import http.client, urllib.parse
params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
conn.request("POST", "/cgi-bin/query", params, headers)
response = conn.getresponse()

请求包是这样:
POST /cgi-bin/query HTTP/1.1
Host: musi-cal.mojam.com
Accept-Encoding: identity
Content-Length: 21
Content-type: application/x-www-form-urlencoded
Accept: text/plain

eggs=2&bacon=0&spam=1


自由添加body:
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = http.client.HTTPConnection("musi-cal.mojam.com:80")
conn.request("POST", "/cgi-bin/query", “a=c&c=d\np=f”, headers)

另一种格式:
conn = http.client.HTTPConnection("aos.isd.com")
header = {"Content-Type":"multipart/form-data; boundary=123456789"}
content = """--123456789
Content-Disposition: form-data; name=value1

versionList
--123456789
Content-Disposition: form-data; name=value2

7792
--123456789--
"""
conn.request("POST", "/cc2/cc/ccInterface.api.php", content, header)

==========================================================

multipart/form-data方式 ( RFC1867 ) HTTP协议大致如下,红色是额外加上的:

POST /msb/WebForm1.aspx HTTP/1.1
Connection: Keep-Alive
Content-Length: 458
Content-Type: multipart/form-data; boundary=-----------------------------7d6bb34502ce[上传方式,边界]
Accept: */*
Expect: 100-continue
Host: localhost
Referer: http://mtv.1ting.com/
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)

-------------------------------7d6bb34502ce[边界这里开头多了2个中划线]
Content-Disposition: form-data; name="key1[表单域名]"

ffffffffffffffffffffffffffffffffffffffffffffffffffffff[表单域值]
-------------------------------7d6bb34502ce[边界这里开头多了2个中划线]
Content-Disposition: form-data; name="file[文件类型表单域名]"; filename="d:\1.rar[文件名]"
Content-Type: application/x-msdownload[文件类型]

[文件内容]
-------------------------------7d6bb34502ce--[最后一个边界,开头和结尾都多了2个中划线




---------------------------------------------------------

import http.client, urllib.parse



def buidcontent(boundary):
    jsonObject = '''{
        "appkey" : "y6Vrn2xQvq",
        "sealType" : 1,
        "docName" : "test.pdf",
        "rules" : [
            {
                "type" : 1,
                "signetId" : "87b1ec0f-38f6-47ea-8e47-7f001b4ed657",
                "offset" : [1668, 2828],
                "page" : 0
            }
            ]
        }'''
    content = '--' + boundary 
    content += "\r\n"
    content += 'Content-Disposition: form-data; name="json"'
    content += "\r\n"
    content += 'Content-Type: text/plain;charset=UTF-8'
    content += "\r\n\r\n"
    content += jsonObject
    content += "\r\n\r\n" 
    
    content += '--' + boundary
    content += "\r\n"
    content += 'Content-Disposition: form-data; name="file";filename="%s"' % "test.pdf"
    content += "\r\n"
    content += 'Content-Type: application/octet-stream'
    content += "\r\n"
    content += 'Content-Transfer-Encoding: binary'
    content += "\r\n\r\n"
    content = content.encode("utf-8")
    fd = open("dzqz.pdf", 'rb')    

    content += fd.read()

    fd.close()
    content += b"\r\n\r\n"
    end = '--' + boundary + "--\r\n"

    end = end.encode("utf-8")
    content += end
    
    return content
        


def testDzqz():
    conn = http.client.HTTPConnection("10.123.25.22:8080")
    boundary = "dzqz"
    header = {"Content-Type" : "multipart/form-data; boundary=%s" % boundary}    
    content = buidcontent(boundary)
    conn.request("POST", "/smp/gateway/signature", content, header)
    response = conn.getresponse()
    print(response.code)
    print(response.headers)
    print(response.read().decode('utf8'))
    
if __name__ == "__main__":
    testDzqz()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值