URL: https://linuxfr.org/forums/general-hors-sujets/posts/poster-sur-la-tribune-en-python-3 Title: Poster sur la tribune en Python (3) Authors: Marotte ⛧ Date: 2014年07月08日T22:54:24+02:00 License: CC By-SA Tags: python, tribune et coincoin Score: -1 Bonjour, Je souhaite programmer un bot pour la tribune. Récupérer les posts ne me pose pas de problème mais la méthode à employer pour poster représente pour moi un travail que je ne crois pas pouvoir réaliser seul. Dans un premier temps je n'essaye pas de poster mais de récupérer l'index de la tribune en étant authentifié. Voici le code que je test, c'est inspiré d'un exemple trouvé sur stackoverflow (pour les commentaires...) : ```python from http.client import HTTPSConnection from base64 import b64encode from config import * from log import * #This sets up the https connection c = HTTPSConnection(NAME) #we need to base 64 encode it #and then decode it to acsii as python 3 stores it as a byte string auth = LOGIN+":"+PASS auth_b = bytes(auth, "utf-8") userAndPass = b64encode(auth_b).decode("ascii") headers = { 'Authorization' : 'Basic %s' % userAndPass } #then connect c.request('GET', '/'+PAGE, headers=headers) #get the response back res = c.getresponse() # at this point you could check the status etc # this gets the page text data = res.read() print (headers) print (res.headers) print (data.decode("utf-8")) ``` Les variables LOGIN & PASS sont mes identifiant (créés pour l'occasion...) sur le site linuxfr.org le reste des variables utilisées : ```python NAME = "linuxfr.org" PAGE = "board" ``` et voici la sortie (tronquée) produite par celui-ci : ``` {'Authorization': 'Basic R3J1bXB5QmVhcjo0N2F2MTI='} Server: nginx/1.2.1 Date: 2014年7月08日 20:42:21 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=5 Status: 200 OK X-XSS-Protection: 1; mode=block X-Content-Type-Options: nosniff X-Served-By: 26038 X-Revision: 02ef1c2697abd7765483ff8c897fea909d8aa2f7 ETag: "3ef6beb60f30f90505ea5b18a3b25c7f" Cache-Control: max-age=0, private, must-revalidate X-Request-Id: 07c874a9-5664-45fb-a9b7-f17a02ec8c20 X-Runtime: 0.069802 X-Content-Type-Options: nosniff