python上传字符和二进制的文件到PHP服务器

python上传字符和文件到PHP服务器

本主要介绍通过Python向php服务器传数据的代码实现

上传简单的字符串

def send_str_server(self):
        payload = {'key1': 'value1', 'key2': 'value2'}
        r = requests.post("http://httpbin.org/post",  
            data=payload)

介绍:payload 为键值对形式的数据,在服务器的数据的显示为
key1=value1&key2=value2
http://httpbin.org/post 为上传的服务器地址

上传文件

def send_image_server(self):
        data = {"k1" : "v1"}  
        files = {"img" : open("test.png", "rb")}  
        r = requests.post("http://httpbin.org/post", data,    
        files=files)  

介绍:data 为键值对形式的数据,为post请求携带的数据
files 中的img表示的是php服务器中对图片的过滤字段,open中第一个参数为图片的地址,第二个参数表示二进制文件写的权限,http://httpbin.org/post是服务器的地址

posted @   飞航之梦  阅读(103)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示