1. 程式人生 > >python 通過post方式上傳檔案到php伺服器

python 通過post方式上傳檔案到php伺服器

看了網上很多程式碼,都沒有說如何具體的使用poster,試了兩天,終於成功了

通過python呼叫php實現了檔案上傳 與大家分享一下:     首先要通過pip安裝poster(easy_install 也是一樣的):
1 pip install poster

image.py:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!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