Reputation: 347
i'm looking a way to create a simple json server which would only get some data and work with them. Without a deep schemas and etc.
All posted answers in web is to deep, difficult, or using deprecated methods/modules :(
All what i need that is:
Client:
requests.post('http://localhost:80108', json={'data':'test'})
Server:
....
....
....
data = json.loads(rcv_data)
if data['data'] == 'test':
print('Simple test')
Upvotes: 0
Views: 6643
Reputation: 347
Ty for answers. Flask is that what i was looking for.
Server:
from flask import Flask
from flask import request
import json
app = Flask(__name__)
def post_actions(data):
return {
'Action1': func1,
'Action2': func2,
'Action3': func3,
}.get(data['Action'])(data['Data'])
@app.route("/", methods=['GET', 'POST'])
def indx():
if request.method == 'POST':
if request.data:
rcv_data = json.loads(request.data.decode(encoding='utf-8'))
rsp = post_actions(rcv_data)
if rsp:
return rsp
else:
return '200'
else:
return '404'
if __name__ == "__main__":
app.run(host='localhost', port='43560')
Client:
import requests
data = {'Action': 'Action2',
'Data': ['MILK', 'BREAD', 'WATER']}
response = request.post('http://localhost:43560/', json=data)
print(response.text)
>>>'200'
Upvotes: 0
Reputation: 922
So you basically need to exchange jsons with a server? Have you thought of a simple REST API? You can implement one with Flask or even easier with Flask-Restful + standard json
module.
Upvotes: 3
Reputation: 106
This isn't a simple answer but I'd recommend looking at how to make a django api app. It is a good starting point. Then use tutorialspoint to you get you to where you need to be. A previous answer mentioned flask. Flask is very similar to django but I'd recommend django over flask if you plan on building this out as a larger project.
Upvotes: 1
Reputation: 599866
JSON is not the important part here, serializing and deserializing JSON data is done by the standard library json
module.
What you need is a simple server that accepts requests and calls Python code; there are several small frameworks that would fit the bill, Flask is a good option.
Upvotes: 5