Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Python Coder gesucht :)[solved]
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
root_tux_linux
l33t
l33t


Joined: 21 Dec 2003
Posts: 966

PostPosted: Sun Dec 16, 2007 2:40 pm    Post subject: Python Coder gesucht :)[solved] Reply with quote

Hi

Bin seit kurzem ja jetzt Python Anfänger und wollte was kleines schreiben, jedoch hab ich jetzt schon das erste problem.

Ich wollte mittels urllib2 ein Formular bzw URL aufrufen, was teilweise auch klappt.

z.B.

Code:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import urllib2

url ='http://chat6.fidion.de/index.php?http_host=test-chat.mainchat.de&id=&aktion=login'
   response = urllib2.urlopen(url)
   response.close()


Aber folgendes klappt nicht

Code:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import urllib2

Session-ID = raw_input('Bitte SessionID eingeben: ')
U-ID = raw_input('Bitte UID eingeben: ')
Nick = raw_input('Bitte Nickname eingeben: ')

url ='http://chat6.fidion.de/edit.php?http_host=test-chat.mainchat.de&id=' + SessionID + '&aktion=edit&f[u_id]=' + UID + '&f[u_nick]=' + Nick +'&eingabe=Ändern!'
   response = urllib2.urlopen(url)
   response.close()



Kann mir jemand sagen was ich falsch mache?
_________________
Intel Core i7 6700K@4.6GHz, Gigabyte GTX 980 Ti G1, Gigabyte Gaming 7, Hyper X Fury 32GB, 2 TB Samsung EVO 840 Basic
ASUS ROG Swift PG348Q Display
ASUS ROG ASUS G771JW Notebook


Last edited by root_tux_linux on Sun Dec 16, 2007 9:38 pm; edited 1 time in total
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sun Dec 16, 2007 7:21 pm    Post subject: Re: Python Coder gesucht :) Reply with quote

root_tux_linux wrote:
Code:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import urllib2

Session-ID = raw_input('Bitte SessionID eingeben: ')
U-ID = raw_input('Bitte UID eingeben: ')
Nick = raw_input('Bitte Nickname eingeben: ')

url ='http://chat6.fidion.de/edit.php?http_host=test-chat.mainchat.de&id=' + SessionID + '&aktion=edit&f[u_id]=' + UID + '&f[u_nick]=' + Nick +'&eingabe=Ändern!'
   response = urllib2.urlopen(url)
   response.close()



Kann mir jemand sagen was ich falsch mache?

Du hast da hoffentlich nicht wirklich ein "-" in den Variablennamen "Session-ID" und "U-ID"? ;)

außerdem würde ich dir empfehlen das
Code:
url = 'http://chat6.fidion.de/edit.php?http_host=test-chat.mainchat.de&id=' + SessionID + '&aktion=edit&f[u_id]=' + UID + '&f[u_nick]=' + Nick +'&eingabe=Ändern!'
evtl umzuschreiben in
Code:
url = 'http://chat6.fidion.de/edit.php?http_host=test-chat.mainchat.de&id=%s&aktion=edit&f[u_id]=%s&f[u_nick]=%s&eingabe=Ändern!' % (SessionID, UID, Nick)
(ich persönlich finde das mit den Platzhaltern übersichtlicher ;) )
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
root_tux_linux
l33t
l33t


Joined: 21 Dec 2003
Posts: 966

PostPosted: Sun Dec 16, 2007 7:45 pm    Post subject: Reply with quote

Hi

Das mit dem Sessions-ID und U-ID hab ich bemerkt und korrigiert.
Funktioniert trotzdem nicht.

Also als Beispiel:

Ich kann im Firefox einen neuen Tab öffnen und denn die URL eingeben und mit Enter bestätigen:
Code:

http://chat6.fidion.de/schreibe.php?http_host=test-chat.mainchat.de&id=3475234785234907502346523&schreibe.php&text2=TEXT&action=Go!


ID ist in dem Fall frei erfunden.

Wenn ich die URL aber mit urllib2.urlopen aufrufe passiert nichts.

Ich hab keinen schimmer wieso nicht :(

Kannste mir helfen?
_________________
Intel Core i7 6700K@4.6GHz, Gigabyte GTX 980 Ti G1, Gigabyte Gaming 7, Hyper X Fury 32GB, 2 TB Samsung EVO 840 Basic
ASUS ROG Swift PG348Q Display
ASUS ROG ASUS G771JW Notebook
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sun Dec 16, 2007 8:08 pm    Post subject: Reply with quote

Code:
necoro@Devoty QM % python
Python 2.5.1 (r251:54863, Nov 28 2007, 16:56:28)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2 as u
>>> b = "http://chat6.fidion.de/schreibe.php?http_host=test-chat.mainchat.de&id=3475234785234907502346523&schreibe.php&text2=TEXT&action=Go!"
>>> c = u.urlopen(b)
>>> c.info().dict
{'transfer-encoding': 'chunked', 'x-powered-by': 'PHP/4.1.2', 'x-accelerated-by': 'PHPA/1.2p4', 'expires': 'Sun, 16 Dec 2007 19:05:18 GMT', 'server': 'Apache/1.3.27 (Unix) PHP/4.1.2', 'last-modified': 'Sun, 16 Dec 2007 20:05:19 GMT', 'connection': 'close', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'date': 'Sun, 16 Dec 2007 20:05:19 GMT', 'content-type': 'text/html'}

Also bei mir kommt da was an ... :)

/edit: heißt das da wirklich "&schreibe.php" ?
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
root_tux_linux
l33t
l33t


Joined: 21 Dec 2003
Posts: 966

PostPosted: Sun Dec 16, 2007 8:27 pm    Post subject: Reply with quote

Necoro wrote:
Code:
necoro@Devoty QM % python
Python 2.5.1 (r251:54863, Nov 28 2007, 16:56:28)
[GCC 4.1.2 (Gentoo 4.1.2 p1.0.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2 as u
>>> b = "http://chat6.fidion.de/schreibe.php?http_host=test-chat.mainchat.de&id=3475234785234907502346523&schreibe.php&text2=TEXT&action=Go!"
>>> c = u.urlopen(b)
>>> c.info().dict
{'transfer-encoding': 'chunked', 'x-powered-by': 'PHP/4.1.2', 'x-accelerated-by': 'PHPA/1.2p4', 'expires': 'Sun, 16 Dec 2007 19:05:18 GMT', 'server': 'Apache/1.3.27 (Unix) PHP/4.1.2', 'last-modified': 'Sun, 16 Dec 2007 20:05:19 GMT', 'connection': 'close', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'date': 'Sun, 16 Dec 2007 20:05:19 GMT', 'content-type': 'text/html'}

Also bei mir kommt da was an ... :)

/edit: heißt das da wirklich "&schreibe.php" ?


Jap heisst schreibe.php

log dich mal als gast ein test-chat.mainchat.de ^^

Also wenn ich mich als Gast oder Admin einlogge in den Chat und dann den oben genannten Link mit der Session-ID vom Gast oder Admin in ein neues Tab hauen und Enter drücke seh ich im Tab 1 das der Gast oder Admin was geschrieben hat. Ich hab jedoch nur den Link zum schreibe.php aufgerufen und nicht wirklich in Tab1 was "normal" geschrieben.

Wenn ich das gleiche aber mit urllib2.urlopen(url) versuche seh ich im Chat nicht das der Gast oder Admin was geschrieben hätte.
Ein response.read() sagt mir sogar "User wäre nicht angemeldet" aber wenn es mit dem Browser funktioneirt wieso funktioniert es mit urllib2.urlopen(url) nicht?

Den Effekt würde ich gerne nutzen Für einen Bot der eine Nachricht schreibt alle 60 Minuten (Autokick ist in 60 min).
_________________
Intel Core i7 6700K@4.6GHz, Gigabyte GTX 980 Ti G1, Gigabyte Gaming 7, Hyper X Fury 32GB, 2 TB Samsung EVO 840 Basic
ASUS ROG Swift PG348Q Display
ASUS ROG ASUS G771JW Notebook
Back to top
View user's profile Send private message
Anarcho
Advocate
Advocate


Joined: 06 Jun 2004
Posts: 2970
Location: Germany

PostPosted: Sun Dec 16, 2007 8:56 pm    Post subject: Reply with quote

Weil der Browser per Cookie die aktuelle Sitzung dem Server mitteilt. Im PHP Script wird sicher irgendwo session_start() aufgerufen und später per $_SESSION[...] nachgeguckt ob der User angemeldet ist. Dabei fragt der Webserver die Cookies vom Browser ab. Da dieser in der aktuellen Browsersitzung ein solches Cookie angelegt hat ist es auch egal in wievielen Tabs du die Seite öffnest. Bis zum Browser-Schliessen sollte es immer klappen (ausser natrülich nach nem Session-Timeout).

Daher musst du dich wohl oder übel vorher per Python authentifizieren und das Cookie entsprechend weiter verwenden. Oder man schreibt die PHP Seite um das sie auch sessions per session_id parameter verwenden kann, falls noch nicht vorhanden. So oder so musst du dich wohl anmelden.
_________________
...it's only Rock'n'Roll, but I like it!
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sun Dec 16, 2007 9:24 pm    Post subject: Reply with quote

So ... das war zu beachten:

1.) Cookies -> Login und schreiben vom gleichen Client (hier Python)
2.) Umwandlung GET-Variablen in POST-Variablen ...

Code:
from urllib2 import urlopen
from urllib import urlencode
import re

# login
login = urlopen('http://chat6.fidion.de/index.php?http_host=test-chat.mainchat.de&id=&aktion=login').read()
print login

# find id
match = re.search("id=[0-9a-f]+", login)
uid = login[match.start() + 3: match.end()] # +3 : strip "id="
print uid

# print
postvars = urlencode({"id" : uid, "http_host" : "test-chat.mainchat.de", "text2": "this was written from a script", "action" : "Go!" })
print postvars
print urlopen('http://chat6.fidion.de/schreibe.php', postvars).read()


Den "find id" Part könnte man noch verbessern - mir fiel nur gerade nix eleganteres ein

/edit: Die print's sind natürlich nur zum debugging und sollten rausfliegen ;)
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
root_tux_linux
l33t
l33t


Joined: 21 Dec 2003
Posts: 966

PostPosted: Sun Dec 16, 2007 9:38 pm    Post subject: Reply with quote

Danke danke dank :)
_________________
Intel Core i7 6700K@4.6GHz, Gigabyte GTX 980 Ti G1, Gigabyte Gaming 7, Hyper X Fury 32GB, 2 TB Samsung EVO 840 Basic
ASUS ROG Swift PG348Q Display
ASUS ROG ASUS G771JW Notebook
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum