详解python上传文件和字符到PHP服务器
作者:laozhang 时间:2023-10-15 19:02:12
很多朋友在留言区询问关于python上传文件和字符到服务器的问题,现编针对这个给大家整理了一个解决办法。
上传简单的字符串
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是服务器的地址
python post方式 上传文件到php服务器
看了网上很多代码,都没有说如何具体的使用poster,试了两天,终于成功了
通过python调用php实现了文件上传
与大家分享一下:
首先要通过pip安装poster(easy_install 也是一样的):
pip install poster
image.py
#!usr/bin/python
# image.py
# -*- coding=utf-8 -*-
from poster.encode import multipart_encode
import urllib2
import sys
from urllib2 import Request, urlopen, URLError, HTTPError
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
register_openers()
f=open(“C:/Users/User/Pictures/Saved Pictures/test1.jpg”, "rb")
#f=open(sys.argv[1], "rb") 使用sys.argv[1]可调用参数 例如 运行 python image.py C:/Users/User/Pictures/Saved Pictures/test1.jpg
#可将test1.jpg作为参数传入image.py
#"C:/Users/User/Pictures/Saved Pictures/vedio5.jpg"
# headers 包含必须的 Content-Type 和 Content-Length
# datagen 是一个生成器对象,返回编码过后的参数
datagen, headers = multipart_encode({"myFile": f})
# 创建请求对象
request = urllib2.Request("http://localhost/upload_image/upload_image.php", datagen, headers)
try:
response = urllib2.urlopen(request)
print response.read()
except URLError,e:
print e.reason
print e.code
-----
upload_image.php
----
<?php
echo $_FILES['myFile']['name'];
if (isset($_FILES['myFile']))
{
$names = $_FILES["myFile"]['name'];
$arr = explode('.', $names);
$name = $arr[0]; //图片名称
$date = date('Y-m-d H:i:s'); //上传日期
$fp= fopen($_FILES['myFile']['tmp_name'], 'rb');
$type = $_FILES['myFile']['type'];
$filename = $_FILES['myFile']['name'];
$tmpname = $_FILES['myFile']['tmp_name'];
//将文件传到服务器根目录的 upload 文件夹中
if(move_uploaded_file($tmpname,$_SERVER['DOCUMENT_ROOT']."/upload/".$filename)){
echo "upload image succeed";
}else{
echo "upload image failed";
}
}
?>
标签:python,文件,字符,PHP服务器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决vue 给window添加和移除resize事件遇到的坑
2024-04-10 13:45:45
一篇文章了解Python中常见的序列化操作
2023-01-22 05:22:20
python程序需要编译吗
2022-08-18 12:57:13
MySQL内外连接的具体使用
2024-01-17 13:52:49
![](https://img.aspxhome.com/file/2023/3/77223_0s.jpg)
Python海龟turtle基础知识大全以及画图集合实例
2022-05-01 08:12:34
![](https://img.aspxhome.com/file/2023/0/97180_0s.jpg)
Python基于smtplib实现异步发送邮件服务
2021-01-15 07:40:37
Python如何省略括号方法详解
2022-12-11 07:23:26
python解析html提取数据,并生成word文档实例解析
2023-10-19 13:50:38
ASP中Cookies集合使用方法详解
2007-09-14 10:16:00
从mysql读写分离着手提升服务器性能
2024-01-22 06:56:10
![](https://img.aspxhome.com/file/2023/8/65298_0s.png)
python内存管理机制原理详解
2021-05-24 16:19:48
![](https://img.aspxhome.com/file/2023/9/80639_0s.jpg)
用纯CSS3绘制的网站图标
2010-03-28 13:51:00
![](https://img.aspxhome.com/file/UploadPic/20103/28/css-social-media-icons-65s.png)
php字符串截取函数mb_substr用法实例分析
2024-05-02 17:35:09
利用Python实现颜色色值转换的小工具
2021-09-12 07:44:15
![](https://img.aspxhome.com/file/2023/2/133252_0s.png)
Apache DophinScheduler定时调度Python脚本的实现
2021-06-23 15:02:54
![](https://img.aspxhome.com/file/2023/0/93780_0s.png)
自动定时备份sqlserver数据库的方法
2011-11-03 17:28:17
对python cv2批量灰度图片并保存的实例讲解
2022-06-11 18:21:36
golang常用库之配置文件解析库-viper使用详解
2024-02-17 18:36:46
![](https://img.aspxhome.com/file/2023/5/107315_0s.png)
pandas读取CSV文件时查看修改各列的数据类型格式
2023-09-26 16:44:58
python读取有密码的zip压缩文件实例
2021-03-31 20:58:53
![](https://img.aspxhome.com/file/2023/6/127896_0s.jpg)