Python脚本将图像发送到PHP [英] Python script send image to PHP
问题描述
成就:我现在可以从我的客户端发送一个文本数据到服务器。
问题:我的大问题是我怎么能发送图像?
任何意见和建议,非常感激。谢谢。
我的Python脚本:
import urllib2 $ b从urllib $ b导入urlencode
#192.168.5.149是服务器
的url地址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 ()
我的PHP脚本:
<?php
echo $ _POST ['test'];
?>
运行python脚本时,PHP服务器发送了ok这意味着,连接成功。
EDITED
Python客户端:
导入请求
url ='http://messi-fan.org/post'
files = {'file':open 'image.png','rb')}
r = requests.post(url,files = files)
PHP服务器:
<?php
$ file_path =C:\\ xampp\htdocs\server\php\\;
$ file_path = $ file_path.basename($ _FILES ['file'] ['name']);
?>
这是非常容易使用
进口请求
url ='http://messi-fan.org/post '
files = {'file':open('image.png','rb')}
r = requests.post(url,files = files)
和PHP
<?php
print_r($ _ FILES);
move_uploaded_file($ _ FILES [file] [tmp_name],$ _ FILES [file] [name]);
Good day. Can somebody help me. My task is to create an python script (client side) that send image to php (server side).
NOTE: The python script is run in different raspberry pi, and the php server only receive the image via internet.
Achievement: I can now send a text data from my client to server.
Problem: My big problem is how can I send the image?
Any comments and suggestion is very appreciated. Thank You.
My Python Script:
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()
My PHP script:
<?php echo $_POST['test']; ?>
When I run the python script, I got "ok" as send by PHP server. That means, the connection is successful.
EDITED
Python client:
import requests url = 'http://messi-fan.org/post' files = {'file': open('image.png', 'rb')} r = requests.post(url, files=files)
PHP server:
<?php $file_path = "C:\\xampp\htdocs\server\php\\"; $file_path = $file_path.basename( $_FILES['file']['name']); ?>
解决方案You can use requests module for this. it is very easy to use
import requests url = 'http://messi-fan.org/post' files = {'file': open('image.png', 'rb')} r = requests.post(url, files=files)
and in PHP
<?php print_r($_FILES); move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
这篇关于Python脚本将图像发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!