Python sockets, part 1

New releases and general discussions.

Post » Mon Mar 31, 2008 3:05 pm

I originally planned to do this earlier, but I've finally got a week off and I've been looking at Python some more, so here goes.

For the first mini tutorial I'll focus on HTTP connections, primarily requesting web pages and getting data (which can then be parsed).

Step 1, assembling a layout
For this code, insert 2 edit boxes. The first should be called 'Query', and the second 'Result'. Add a button in which will act as the trigger to execute the python script.

Step 2, making a HTTP query script
For this piece of code, we'll be using the HTTPConnection object, which provides an interface for GET/POST without having to set up headers. So begin the script with:

[code:26ccsn3t]import httplib[/code:26ccsn3t]

Because we don't want this to run all the time, define the code below as a function:

[code:26ccsn3t]def SearchGoogle():[/code:26ccsn3t]

The editor will then auto-indent so any following code will not be run every event loop (a mistake I made first time around :wink:).

Python is a very simple language syntax wise, so it's perhaps better to paste the entire function and then explain it:

[code:26ccsn3t]
HTTPCon = httplib.HTTPConnection("www.google.com")
HTTPCon.request("GET", "/search?hl=en&num=10&start=1&safe=on&q=" + Query.Text)

Response = HTTPCon.getresponse()

if Response.reason == "OK":
Data = Response.read()
Result.SetText(Data)

HTTPCon.close()
[/code:26ccsn3t]

HTTPCon is the HTTP connection which is instantiated (or created) from the python object, it is defined with a connection to "www.google.com". The next line builds the search string. We are using GET; the object also supports POST so you can build hi-score engines, or post data through applications. After that, define a response object and check that the query was 'OK'. Then simply read the data and put it into the Result edit box.

Finally, add a condition for button clicked, and use the system action 'Run Script' and simply put in 'SearchGoogle()'.

Step 3, making a POST script
Posting data works nearly exactly the same way, except we must also post parameters.

For those who do not know how posting works, it is basically sending a form to a server, whether a cgi, php or asp script, which can then store the information or otherwise process it.

Here's a small example (suppose you want to subscribe to updates):

[code:26ccsn3t]
Data = "myemail@email.com"

HTTPCon = httplib.HTTPConnection("localhost")
HTTPCon.request("POST", "/Subscribe.php", Data)

Response = HTTPCon.getresponse()

if Response.reason == "OK":
Result.SetText("Subscribed")

HTTPCon.close()
[/code:26ccsn3t]

I couldn't actually find a suitable testing server or script for POST; if anyone finds one let me know. POST can also handle uploading of files to servers, via multi-part. I may touch on this in a future tutorial.

Next though I'll look at direct connections through sockets.

Tim
B
2
G
5
Posts: 26
Reputation: 1,302

Post » Tue Apr 01, 2008 3:02 am

Looking good :)
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518

Post » Sun Apr 20, 2008 8:47 pm

[quote="timtim":cqqglok3]
I couldn't actually find a suitable testing server or script for POST; if anyone finds one let me know. POST can also handle uploading of files to servers, via multi-part. I may touch on this in a future tutorial.

Next though I'll look at direct connections through sockets.

Tim[/quote:cqqglok3]

What do you need to be able to do the POST thing, i may be able to help out.
B
3
S
2
G
5
Posts: 301
Reputation: 2,302

Post » Thu Apr 24, 2008 11:43 am

I have a dual xeon server with 4GB ram lying around in the local DC ;)
B
2
S
2
G
5
Posts: 448
Reputation: 2,546

Post » Mon May 12, 2008 9:44 am

I receive this error when i try the example :(

B
3
S
2
G
5
Posts: 301
Reputation: 2,302


Return to Construct Classic Discussion

Who is online

Users browsing this forum: No registered users and 3 guests