Tak*_*ato 1 php python webserver file-upload http-post
美好的一天.有人可以帮助我.我的任务是创建一个将图像发送到php(服务器端)的python脚本(客户端).
注意:python脚本在不同的raspberry pi中运行,php服务器只通过Internet接收图像.
成就:我现在可以从客户端向服务器发送文本数据.
问题:我的大问题是如何发送图像?
任何意见和建议非常感谢.谢谢.
我的Python脚本:
import urllib2
from urllib import urlencode
# 192.168.5.149 is the ip address of server
url = "http://192.168.5.149/server/server.php"
data = {'test':'OK'}
encoded_data = urlencode(data)
website = urllib2.urlopen(url, encoded_data)
print website.read()
Run Code Online (Sandbox Code Playgroud)
我的PHP脚本:
<?php
echo $_POST['test'];
?>
Run Code Online (Sandbox Code Playgroud)
当我运行python脚本时,我得到了"ok"作为PHP服务器的发送.这意味着,连接成功.
EDITED
Python客户端:
import requests
url = 'http://messi-fan.org/post'
files = {'file': open('image.png', 'rb')}
r = requests.post(url, files=files)
Run Code Online (Sandbox Code Playgroud)
PHP服务器:
<?php
$file_path = "C:\\xampp\htdocs\server\php\\";
$file_path = $file_path.basename( $_FILES['file']['name']);
?>
Run Code Online (Sandbox Code Playgroud)
您可以使用请求模块.这是非常容易使用
import requests
url = 'http://messi-fan.org/post'
files = {'file': open('image.png', 'rb')}
r = requests.post(url, files=files)
Run Code Online (Sandbox Code Playgroud)
在PHP中
<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
Run Code Online (Sandbox Code Playgroud)