How to create simple python webservice

Create file helloworld.py

#!/usr/bin/env python

”’
Created on Apr 10, 2013

@author: kowalczy
”’
import web

urls = (
‘/hello/(.*)’, ‘hello’,
‘/.*’ , ‘index’
)
usage=”Example usage : \n”+”http://server:port/hello/name \n”

class index:
def GET(self):
return “\n”+ usage

class hello:
def GET(self, name):
if name==”:
return “Hello unknown”
else:
return “Hello ” + name

if __name__ == “__main__”:
app = web.application(urls, globals())
app.run()

start service :
python helloworld.py 2345
run request:
http://hostname:2345/hello/Jacek

The response should be
Hello Jacek

Advertisements

How to send GET and POST request with wget

GET request

wget -S –header=”Accept-Encoding: gzip, deflate” -O response.txt http://server:8080/application/params

POST request

wget -S –header=”Accept-Encoding: gzip, deflate” –header=’Accept-Charset: UTF-8′ –header=’Content-Type: application/json’ -O response.json –post-data ‘{“param1″:”myspecialValue1″,”param2″:”my special value 2”}’ http://server:8080/application2/params2