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

yipeiwu_com6年前服务器

很多朋友在留言区询问关于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服务器的代码实现的两种方式,如果大家还有更好的内容可以在下方留言给我们,一起交流一下。

相关文章

尝试使用Python多线程抓取代理服务器IP地址的示例

这里以抓取 http://www.proxy.com.ru 站点的代理服务器为例,代码如下: #!/usr/bin/env python #coding:utf-8 import u...

Python Web程序部署到Ubuntu服务器上的方法

Python Web程序部署到Ubuntu服务器上的方法

在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代...

PHP实现的服务器一致性hash分布算法示例

本文实例讲述了PHP实现的服务器一致性hash分布算法。分享给大家供大家参考,具体如下: <?php /** * 对服务器进行一致性hash分布算法 */ clas...

用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤

用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤

本人主要是做ASP.NET开发的,但有时候也会接触到PHP,而且我认为PHP有很多源码值得学习,我们不是学习PHP代码的写法,而是学习源码的实现思路,或者免强叫为算法。 作为一名非专业的...

【不二】关于affiliate和niche站运营的6条建议

【不二】关于affiliate和niche站运营的6条建议

今天圈友@不二  来给大家分享一些affiliate和英文niche站的的入门玩法,给新手做个科普。 niche,中文一般翻译为利基,可以理解成长尾细分市场,这些细分领域一般来说,市场...