forked from twillis/webobtoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Willis
committed
May 25, 2013
1 parent
326abef
commit 71cb478
Showing
8 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
uploading files example | ||
""" | ||
from webobtoolkit.client import Client, client_pipeline | ||
from webob import Request, Response | ||
|
||
|
||
|
||
|
||
def application(environ, start_response): | ||
"""this application merely spits out the keys of the form that was | ||
posted. we are using webob Request and Response for brevity | ||
""" | ||
request = Request(environ) | ||
return Response(str(request.POST.keys()))(environ, start_response) | ||
|
||
client = Client(pipeline=client_pipeline(application)) | ||
print client.post("/", files=dict(file1=("myfile.txt", | ||
"this is a file containing this text"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
""" | ||
enable logging of request and response | ||
""" | ||
from webobtoolkit import client | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
c = client.Client(client.client_pipeline(logging=True, log_level=logging.DEBUG)) | ||
c.get("http://google.com") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
passing parameters as a form post | ||
""" | ||
from webobtoolkit.client import Client | ||
client = Client() | ||
|
||
|
||
def assert_success(request, response): | ||
""" | ||
if response status != 200 then raise an error | ||
""" | ||
|
||
if response.status_int != 200: | ||
raise Exception("Did not get a valid response from %s" % request.url) | ||
|
||
|
||
print client.post("http://ajax.googleapis.com/ajax/services/search/web", | ||
post=dict(v="1.0", q="define: HTTP"), | ||
assert_=assert_success) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
passing parameters as a query string | ||
""" | ||
from webobtoolkit.client import Client | ||
client = Client() | ||
result = client.get("http://ajax.googleapis.com/ajax/services/search/web", | ||
query_string=dict(v="1.0", q="define: HTTP")).json | ||
for k, v in result.items(): | ||
print k, ":", v | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
""" | ||
getting a response from wikipedia.org | ||
""" | ||
from webobtoolkit.client import Client | ||
client = Client() | ||
print client.get("http://en.wikipedia.org/wiki/HTTP") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
getting a response from a wsgi application | ||
""" | ||
from webobtoolkit import client | ||
|
||
|
||
def application(environ, start_response): | ||
""" | ||
most python webframeworks provide a way to expose your web | ||
application as a WSGI app. consult your framework documentation | ||
for details. | ||
""" | ||
status = "200 OK" # HTTP messages have a status | ||
body = "Hello World" # HTTP messages have a body | ||
|
||
# HTTP messages have headers to describe various things, at a | ||
# minimum describing the type(Content-Type) and the length of the | ||
# content(Content-Length) | ||
headers = [("Content-Type", "text/plain"), | ||
("Content-Length", | ||
str(len(body)))] | ||
|
||
start_response(status, headers) # calling the function passed in | ||
# with the status and headers of | ||
# the HTTP Response Message | ||
|
||
return [body] # returning a list containing the body of the HTTP | ||
# Response Message | ||
|
||
print client.Client(pipeline=client.client_pipeline(application)).get("/") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters