Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44107 > unrolled thread
| Started by | webmaster@terradon.nl |
|---|---|
| First post | 2013-04-22 13:09 -0700 |
| Last post | 2013-04-25 07:12 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
How to get JSON values and how to trace sessions?? webmaster@terradon.nl - 2013-04-22 13:09 -0700
Re: How to get JSON values and how to trace sessions?? Chris Angelico <rosuav@gmail.com> - 2013-04-23 06:47 +1000
Re: How to get JSON values and how to trace sessions?? Tim Roberts <timr@probo.com> - 2013-04-23 23:25 -0700
Re: How to get JSON values and how to trace sessions?? Roozbeh <roozbeh73@gmail.com> - 2013-04-25 07:12 -0700
| From | webmaster@terradon.nl |
|---|---|
| Date | 2013-04-22 13:09 -0700 |
| Subject | How to get JSON values and how to trace sessions?? |
| Message-ID | <c370e0e6-8100-4fa8-8eb6-81d5f622031a@googlegroups.com> |
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 i 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?
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'))
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-04-23 06:47 +1000 |
| Message-ID | <mailman.937.1366663657.3114.python-list@python.org> |
| In reply to | #44107 |
On Tue, Apr 23, 2013 at 6:09 AM, <webmaster@terradon.nl> 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 suppose i 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
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2013-04-23 23:25 -0700 |
| Message-ID | <pfuen8944r5lsbm1nqenfmfe7dqf2qggoh@4ax.com> |
| In reply to | #44107 |
webmaster@terradon.nl 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 suppose i have to save a cookie with the sessionID from the webserver? Yes. The server will send a Set-Cookie: header with your first response. You just need to return that in a Cookie: header with every request you make after that. >Are their other ways to keep control over which players sends the gamedata? Not sure what you mean. If the web site requires cookies, this is what you have to do. -- Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Roozbeh <roozbeh73@gmail.com> |
|---|---|
| Date | 2013-04-25 07:12 -0700 |
| Message-ID | <b9780326-9561-42d0-b5fc-0a73d58cdff4@googlegroups.com> |
| In reply to | #44107 |
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/")
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web