Question

I'm beginner for the client server programming (Specially I have no experience with python server side programing). I'm trying to send some data to python server which uses cherry.py HTTP framework. following Code shows the server implementation.

import cherrypy
from cherrypy import request

class test:
    @cherrypy.expose
    def index(self):
        print request.params['port']
        return "Done";


if __name__ == "__main__":
    cherrypy.config.update( {'server.socket_host':"0.0.0.0", 'server.socket_port':8181 } )
    cherrypy.quickstart(test())  

I need to use C# client for send the POST type request.here is the Method that I'm using to send the data.

    private void applicationOn()
    {

        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["port"] = "hello";

            var ret = client.UploadValues("http://localhost:8181/", "POST", values);
            MessageBox.Show(Encoding.ASCII.GetString(ret));
        }
    }

even though this client program is working fine with the PHP, it is not working with python. when I'm trying to send the request it gives me this error

"The remote server returned an error: (400) Bad Request." this is the stack trace.

    System.Net.WebException was unhandled
      HResult=-2146233079
      Message=The remote server returned an error: (400) Bad Request.
      Source=System
      StackTrace:
           at System.Net.WebClient.UploadValues(Uri address, String method, NameValueCollection data)
           at System.Net.WebClient.UploadValues(String address, String method, NameValueCollection data)
           at HTTP_Client.Form1.applicationOn() in c:\Users\pusalk\Documents\Visual Studio 2012\Projects\HTTP Client\HTTP Client\Form1.cs:line 125
           at HTTP_Client.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\pusalk\Documents\Visual Studio 2012\Projects\HTTP Client\HTTP Client\Form1.cs:line 29
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at HTTP_Client.Program.Main() in c:\Users\pusalk\Documents\Visual Studio 2012\Projects\HTTP Client\HTTP Client\Program.cs:line 19
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

Please help me to sort out the problem

Was it helpful?

Solution

The CherryPy handler is not expecting any parameters, try with this:

import cherrypy
from cherrypy import request

class test:
    @cherrypy.expose
    def index(self, **params):
        # all the request parametes goes into the params dictionary.
        # in case that no port is defined return None
        print params.get('port', None) 
        return "Done";


if __name__ == "__main__":
    cherrypy.config.update( {'server.socket_host':"0.0.0.0",
                             'server.socket_port':8181 })
    cherrypy.quickstart(test())  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top