रास्पबेरी पीआई से डब्ल्यूसीएफ सेवा में अपलोड छवि के लिए पायथन क्लाइंट

मेरे पास एक डब्ल्यूसीएफ सेवा चल रही है जो एक छवि फ़ाइल लेती है और इसे स्थानीय फाइल सिस्टम में संग्रहीत करती है। यह एक कंसोल अनुप्रयोग द्वारा होस्ट किया जाता है और स्थानीय कंप्यूटर में चलता है। जिसके लिए कोड नीचे दिया गया है।


    public interface IImageUpload
        [WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}")]
        void FileUpload(string fileName, Stream fileStream);



public class ImageUploadService : IImageUpload
        public void FileUpload(string fileName, Stream fileStream)
        FileStream fileToupload = new FileStream("C:\\ImageProcessing\\Images\\Destination\\" + fileName, FileMode.Create);
        byte[] bytearray = new byte[5000000];
        int bytesRead, totalBytesRead = 0;
            bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);
        fileToupload.Write(bytearray, 0, bytearray.Length);


होस्ट करने के लिए कंसोल program.cs

static void Main(string[] args)
        string baseAddress = "";
        WebHttpBinding wb = new WebHttpBinding();
        wb.MaxBufferSize = 4194304;
        wb.MaxReceivedMessageSize = 4194304;
        wb.MaxBufferPoolSize = 4194304;

        ServiceHost host = new ServiceHost(typeof(ImageUploadService), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(IImageUpload), wb, "").Behaviors.Add(new WebHttpBehavior());
        //host.AddServiceEndpoint(typeof(IImageUpload), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
        Console.WriteLine("Host opened");

अब मेरे पास एक सी # क्लाइंट है जो इस सेवा के साथ बातचीत को अपलोड करने के लिए एक बटन के साथ बस एक साधारण विंडोज़ फॉर्म है। यह अपनी स्थानीय प्रणाली से एक फाइल लेता है और इसे सेवा में भेजता है। कोड नीचे जैसा है।

 private void button1_Click(object sender, EventArgs e)
        byte[] bytearray = null;

        string name = "PictureX.jpg";

        Image im = Image.FromFile("C:\\ImageProcessing\\Images\\Source\\Picture1.jpg");//big file

        bytearray = imageToByteArray(im);

        string baseAddress = "";
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + name);
        request.Method = "POST";
        request.ContentType = "text/plain";
        request.ContentLength = bytearray.Length;

        Stream serverStream = request.GetRequestStream();
        serverStream.Write(bytearray, 0, bytearray.Length);
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            int statusCode = (int)response.StatusCode;
            StreamReader reader = new StreamReader(response.GetResponseStream());

    public byte[] imageToByteArray(Image imageIn)
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        return ms.ToArray();

Both of them are working fine when I am running it on separate visual studio inside the same laptop. I do not have an app.config or web.config in my service. Everything is written in the code. Entire code can be found in the below link. https://github.com/logrcubed/ImageprocessingAlgorithm_WCF

Now I hook in a raspberry pi to the same WiFi (read local LAN) and want to write a python script to upload an image from a local folder in Pi to WCF service similar to my C# client. *How to achieve this? What is the python code for getting this done? I am new to python. So any help is appreciated.*

I tried the below approaches as a hit and trial most of which gave 404 errors. I do not understand whether these are right or not. Could anyone help me in converting the webrequest in C# to python? Poster library

# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2

# Start the multipart/form-data encoding of the file "DSC0001.jpg"
# "image1" is the name of the parameter, which is normally set
# via the "name" parameter of the HTML <input> tag.

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({"image1": open("/home/pi/test.jpg", "rb")})

# Create the Request object
request = urllib2.Request("", datagen, headers)
# Actually do the request, and get the response
print urllib2.urlopen(request).read()


import urllib2
import MultipartPostHandler
params = {'file':open( "/home/pi/test.jpg" , 'rb')}
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
req = urllib2.Request( "" , params)
text_response = urllib2.urlopen(req).read().strip()


import requests

url = ''
headers = {'content-type': 'text/plain'}
files = {'file': open('/home/pi/test.jpg', 'rb')}
r = requests.post(url, files=files, headers=headers)
जोड़ा संपादित
विचारों: 1

कोई जवाब नहीं है