Python sockets, part 1

This forum is currently in read-only mode.
From the Asset Store
Smart UI Part 1 by Epic Stock Media is a one-of-a-kind collection of modern user interface sound effects.
  • 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:

    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 ).
    
    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]
    
    [i]HTTPCon[/i] 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()'.
    
    [b]Step 3, making a POST script[/b]
    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
  • Looking good

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

    What do you need to be able to do the POST thing, i may be able to help out.

  • I have a dual xeon server with 4GB ram lying around in the local DC

  • I receive this error when i try the example

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)