詳解python上傳文件和字符到PHP服務(wù)器
很多朋友在留言區(qū)詢問關(guān)于python上傳文件和字符到服務(wù)器的問題,現(xiàn)編針對這個(gè)給大家整理了一個(gè)解決辦法。
上傳簡單的字符串
def send_str_server(self): payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload)
介紹:payload 為鍵值對形式的數(shù)據(jù),在服務(wù)器的數(shù)據(jù)的顯示為
key1=value1&key2=value2
http://httpbin.org/post 為上傳的服務(wù)器地址
上傳文件
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 為鍵值對形式的數(shù)據(jù),為post請求攜帶的數(shù)據(jù)
files 中的img表示的是php服務(wù)器中對圖片的過濾字段,open中第一個(gè)參數(shù)為圖片的地址,第二個(gè)參數(shù)表示二進(jìn)制文件寫的權(quán)限,http://httpbin.org/post是服務(wù)器的地址
python post方式 上傳文件到php服務(wù)器
看了網(wǎng)上很多代碼,都沒有說如何具體的使用poster,試了兩天,終于成功了
通過python調(diào)用php實(shí)現(xiàn)了文件上傳
與大家分享一下:
首先要通過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]可調(diào)用參數(shù) 例如 運(yùn)行 python image.py C:/Users/User/Pictures/Saved Pictures/test1.jpg #可將test1.jpg作為參數(shù)傳入image.py #"C:/Users/User/Pictures/Saved Pictures/vedio5.jpg" # headers 包含必須的 Content-Type 和 Content-Length # datagen 是一個(gè)生成器對象,返回編碼過后的參數(shù) datagen, headers = multipart_encode({"myFile": f}) # 創(chuàng)建請求對象 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']; //將文件傳到服務(wù)器根目錄的 upload 文件夾中 if(move_uploaded_file($tmpname,$_SERVER['DOCUMENT_ROOT']."/upload/".$filename)){ echo "upload image succeed"; }else{ echo "upload image failed"; } } ?>
以上就是小編親測的關(guān)于python上傳和文件和字符到PHP服務(wù)器的代碼實(shí)現(xiàn)的兩種方式,如果大家還有更好的內(nèi)容可以在下方留言給我們,一起交流一下。
相關(guān)文章
pyttsx3實(shí)現(xiàn)中文文字轉(zhuǎn)語音的方法
今天小編就為大家分享一篇pyttsx3實(shí)現(xiàn)中文文字轉(zhuǎn)語音的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12使用python如何將數(shù)據(jù)集劃分為訓(xùn)練集、驗(yàn)證集和測試集
這篇文章主要介紹了使用python如何將數(shù)據(jù)集劃分為訓(xùn)練集、驗(yàn)證集和測試集問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09pytorch中的scatter_add_函數(shù)的使用解讀
這篇文章主要介紹了pytorch中的scatter_add_函數(shù)的使用解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python中pd.cut()與pd.qcut()的對比及示例
本文主要介紹了python中pd.cut()與pd.qcut()的對比及示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Python日期時(shí)間模塊datetime詳解與Python 日期時(shí)間的比較,計(jì)算實(shí)例代碼
python中的datetime模塊提供了操作日期和時(shí)間功能,本文為大家講解了datetime模塊的使用方法及與其相關(guān)的日期比較,計(jì)算實(shí)例2018-09-09