How to Login to a Page Where Logins are Optional
<p>I'm trying to login to a webpage however, the problem--as far as I cant tell--is that my username and password aren't getting passed by <code>post</code> request. </p>

<p>So far I've tried: </p>

<pre><code>with requests.Session() as s:
    p = s.post('http://www.marinetraffic.com/en/users/ajax_user_menu?', headers=user_agent1, data=payload)
    r = s.get('http://www.marinetraffic.com/en/ais/index/port_moves/all/include_anchs:yes/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:yes/time_interval:1474912018_1474998300/per_page:50/port:2341/portname:MUMBAI')
</code></pre>

<p>Where  <code>user-agent</code> is my user agent, and <code>payload</code> are my valid login credentials. With both the <code>post</code> and <code>get</code> requests I get a <code>200</code> response, however the page I see is the same as if I hadn't logged in, i.e. the results aren't filtered the way the URL indicates they should be.   </p>

<p>How do I make sure that I'm logged in?</p>

<p>It is actually completely the wrong url you posted, you need to post to <em><a href="https://www.marinetraffic.com/en/users/ajax_login" rel="nofollow">https://www.marinetraffic.com/en/users/ajax_login</a></em> and set the correct headers:</p>

<pre><code>data = [("_method", (None, "POST")), ("data[email]", (None, "you@mail.com")),
        ("data[password]", (None, "pass"))]
with requests.Session() as s:

    s.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
                  "X-Requested-With": "XMLHttpRequest"})
    p = s.post("https://www.marinetraffic.com/en/users/ajax_login", files=data)
    print(p.content)
    print(s.get("http://www.marinetraffic.com/en/ajax_user_settings/get_user_settings").json())
</code></pre>

<p>For the record this would also work:</p>

<pre><code>with requests.Session() as s:
    data = dict(email="you@mail.com", password="pass", _method="POST")
    s.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)",
                  "X-Requested-With": "XMLHttpRequest"})
    p = s.post("https://www.marinetraffic.com/en/users/ajax_login", data=data)
    print p.content
    print(s.get("http://www.marinetraffic.com/en/ajax_user_settings/get_user_settings").json())
</code></pre>

python python-requests