I'm pretty new to this so any help would be greatly appreciated. I have two pieces of code and there is a missing link which I'm hoping someone can fill in. I'm new to both platforms and I don't have much experience with back-end nor with web architecture.
So below is the first bit of code, it's an adapted python server and it shows me a formatted JSON string when I enter http://localhost:8000 into the browser. This is great. It's what I want to see.
#!/usr/bin/python
"""
Save this file as server.py
>>> python server.py 0.0.0.0 8001
serving on 0.0.0.0:8001
or simply
>>> python server.py
Serving on localhost:8000
You can use this to test GET and POST methods.
"""
import SimpleHTTPServer
import SocketServer
import logging
import cgi
import sys
import json
import simplejson
import time
if len(sys.argv) > 2:
PORT = int(sys.argv[2])
I = sys.argv[1]
elif len(sys.argv) > 1:
PORT = int(sys.argv[1])
I = ""
else:
PORT = 8000
I = ""
current_milli_time = lambda: int(round(time.time() * 1000))
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.warning("======= GET STARTED =======")
logging.warning(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.warning("======= POST STARTED =======")
logging.warning(self.headers)
data_string = self.rfile.read(int(self.headers['Content-Length']))
#print "data_string: "
#print data_string
data = simplejson.loads(data_string)
#print "json data: "
#print data
response = {}
response = {"time": current_milli_time()}
# Send a repsonse
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data))
Handler = ServerHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "@rochacbruno Python http server version 0.1 (for testing purposes only)"
print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT)
httpd.serve_forever()
I have a WPF/C# Application which contains a background worker and I have previously used this to get JSON string from a REST service.
public void getData(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
var url = "http://localhost:xxxx/";
var syncClient = new WebClient();
while (true)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(100);
var content = syncClient.DownloadString(url);
Console.WriteLine(content);
}
}
}
So I pretty much need some direction on how to proceed, how do I get the JSON string into my WPF/C# Application? I hope this is clear enough, let me know if you need more info.
Edit: I have a feeling I need to get the current python server to post to another server which is RESTful which I can then HTTP GET the JSON string.
Cheers!