0

I know my question may be is not really good. but as a person who is new with python I have a question:

I wrote a code with python that make me login to my page:

import urllib, urllib2, cookielib

email = 'myuser'
password = 'mypass'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'email' : email, 'password' : password})
opener.open('http://test.com/signin', login_data)
resp = opener.open('http://test.com/dashboard')
print resp.read()

and now I an connected to my page....

This is my tamper data when I want to send message to site:

How can I send hello with python now? could you possibly complete the code and tell me how it is done?

UPDATE

I changed my code like so:

import requests

url1 = 'http://test.com/signin'
data1 = {
    'email': 'user',
    'password': 'pass',
    }
requests.post(url1, data=data1)

url2 = 'http://test.com/dashboard'
data2 = {
    'post_temp_id': '61jm5by188',
    'message': 'hello',
    }
requests.post(url2, data=data2)

But no result

Thank you

Community
  • 1
  • 1
MLSC
  • 5,872
  • 8
  • 55
  • 89

1 Answers1

1

Although you could start off using urllib, you'll be happier using requests. How to use the POST method:

import requests
resp = requests.post('http://test.com/dashboard', data={'post_temp_id': '61jm5by188', 'message': 'hello'})

Pretty simple, right? Dictionaries can be used to define headers, cookies, and whatever else you'd want to include in your request. Most requests will only need a single line of code.

EDIT1: I don't have a test.com account, but you may try using this script to test out the POST method. This website will echo what you submit in the form, and the script should get you the same response:

import requests
resp = requests.post('http://hroch486.icpf.cas.cz/cgi-bin/echo.pl',
                     data={'your_name': 'myname',
                           'fruit': ['Banana', 'Lemon', 'Plum']})
idx1 = resp.text.index('Parsed values')
idx2 = resp.text.index('No cookies')
print resp.text[idx1:idx2]

From the HTML you received, here's what you should see:

Parsed values</H2>
<UL>
<LI>fruit:
<UL compact type=square>
<LI>Banana
<LI>Lemon
<LI>Plum
</UL>
<LI>your_name = myname
</UL>
<H2>

EDIT2: How to use a session object:

from requests import Session
s = Session()
# Don't just copy this; set your data accordingly...
url1 = url2 = data1 = data2 = ...
resp1 = s.post(url1, data=data1)
resp2 = s.post(url2, data=data2)

The advantage of a session object is that it stores any cookies and headers from previous responses.

Samba
  • 199
  • 2
  • 6
  • But when I add this line to my code it doesn't work...could you possibly tell me how to correct the code? thank yoiu – MLSC Jun 08 '14 at 16:03
  • When you say it doesn't work, do you mean that you didn't get a response? When you use the `post` method, a `Response` object is returned. Store it (as I did in my example) and look at the status code (`resp.status_code`). You also might want to take a look at the [reference pages](http://docs.python-requests.org/en/latest/). Hope this helps. – Samba Jun 08 '14 at 17:38
  • yes...It returns 200 to me... Is my update code true? – MLSC Jun 08 '14 at 17:47
  • It doesn't post hello word on be half of me in test.com – MLSC Jun 08 '14 at 17:50
  • 1
    Hmm. I'm not familiar with test.com, but you might need to add appropriate headers or cookies. I'd suggest using a `Session` object to log in instead of the plain `requests.post`. You can read about it [here](http://docs.python-requests.org/en/latest/user/advanced/#session-objects). – Samba Jun 08 '14 at 18:01
  • Thank you ... I would check – MLSC Jun 08 '14 at 18:07