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

W

webmaster

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'))
 
C

Chris Angelico

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
 
R

Roozbeh

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/")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top