How to get JSON values and how to trace sessions??

Discussion in 'Python' started by webmaster@terradon.nl, Apr 22, 2013.

  1. Guest

    Hi all,
    from python I post data to a webpage using urllib and can print that content.
    See code below.

    But now i am wondering how to trace sessions? it is needed for a multiplayer game, connected to a webserver. How do i trace a PHP-session? I suppose ihave to save a cookie with the sessionID from the webserver? Is this possible with Python? Are their other ways to keep control over which players sends the gamedata?

    Secondly, can i handle JSON values? I know how to create them serverside, but how do i handle that response in python?

    Thank you very much for any answer!


    Code:
    import urllib.request
    import urllib.parse

    user = 'user'
    pw = 'password'

    login_url = 'http://www.xxxxxxxx.nl/test/index.php'

    data = urllib.parse.urlencode({'user': user, 'pw': pw})
    data = data.encode('utf-8')
    # adding charset parameter to the Content-Type header.
    request = urllib.request.Request(login_url)
    request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")

    f = urllib.request.urlopen(request, data)
    print(f.read().decode('utf-8'))
    , Apr 22, 2013
    #1
    1. Advertising

  2. On Tue, Apr 23, 2013 at 6:09 AM, <> wrote:
    > But now i am wondering how to trace sessions? it is needed for a multiplayer game, connected to a webserver. How do i trace a PHP-session? I supposei have to save a cookie with the sessionID from the webserver? Is this possible with Python? Are their other ways to keep control over which players sends the gamedata?
    >
    > Secondly, can i handle JSON values? I know how to create them serverside,but how do i handle that response in python?


    Python has a JSON module that should do what you want:
    http://docs.python.org/3.3/library/json.html

    I don't know the details of cookie handling in Python, but this looks
    to be what you want:

    http://docs.python.org/3.3/library/http.cookiejar.html#http.cookiejar.CookieJar

    Tip: The Python docs can be searched very efficiently with a web
    search (eg Google, Bing, DuckDuckGo, etc). Just type "python" and
    whatever it is you want - chances are you'll get straight there.

    ChrisA
    Chris Angelico, Apr 22, 2013
    #2
    1. Advertising

  3. Roozbeh Guest

    You need to handle the cookie that comes with the server response. Example code:

    import urllib2
    opener = urllib2.build_opener()
    opener.addheaders.append(('Cookie', 'cookiename=cookievalue'))
    f = opener.open("http://example.com/")
    Roozbeh, Apr 25, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    231
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    317
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    257
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    155
    David Karr
    Jun 17, 2013
Loading...

Share This Page