Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in requests.post
#1
I am making a project and for that, I am making a python program. The program uses requests.post to get access to some info. However, whenever I run it, it gives a traceback.

The error is that the remote end disconnects without response. How can I solve this problem?
I need help ASAP.

Here's the code below

import requests,re
import xml.etree.ElementTree as ET

#def getCellTowerInfo():
print ("Logging in to router home page")    
baseurl = 'http://192.168.0.1/'
url = baseurl + 'login.asp'
payload = {'ID':'admin','PASSWORD':'admin','REDIRECT':'index.asp','REDIRECT_ERR':'login.asp'}
response = requests.get(url, data = payload)

print (response)
Reply
#2
(Sep-17-2018, 05:29 PM)debanilroy Wrote:
...
payload = {'ID':'admin','PASSWORD':'admin','REDIRECT':'index.asp','REDIRECT_ERR':'login.asp'}
response = requests.get(url, data = payload)
...
print (response)
  • With payload, you must use post, not get.
  • data= is used with serialized payload - either use data=json.dumps(payload) or json=payload
  • When you login, you need to store aside session authorization. If you use
    session = requests.Session()
    session.post(url, ...) 
    the session object will manage that for you
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Thanks for the help!!!
However, I have not been able to solve the problem still. I am a beginner python programmer and so I have little knowledge about it. However, I can understand what's happening by reading a code.

I've changed the code as per your advice. Is it how you wanted now?

import requests,re
import xml.etree.ElementTree as ET
import json
#def getCellTowerInfo():
print ("Logging in to router home page")    
baseurl = 'http://192.168.0.1/'
url = baseurl + 'login.asp'
payload = {'ID':'admin','PASSWORD':'admin','REDIRECT':'index.asp','REDIRECT_ERR':'login.asp'}
response = requests.Session()
response = response.post(url, data = json.dumps(payload) )
print (response)
On running this, the error which pops out is...

Error:
Logging in to router home page Traceback (most recent call last): File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "<string>", line 2, in raise_from File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1197, in getresponse response.begin() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 297, in begin version, status, reason = self._read_status() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 266, in _read_status raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\adapters.py", line 445, in send timeout=timeout File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\util\retry.py", line 367, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\packages\six.py", line 685, in reraise raise value.with_traceback(tb) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen chunked=chunked) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "<string>", line 2, in raise_from File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 1197, in getresponse response.begin() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 297, in begin version, status, reason = self._read_status() File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\http\client.py", line 266, in _read_status raise RemoteDisconnected("Remote end closed connection without" urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "get_cell_tower_data.py", line 10, in <module> response = response.post(url, data = json.dumps(payload) ) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 559, in post return self.request('POST', url, data=data, json=json, **kwargs) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 512, in request resp = self.send(prep, **send_kwargs) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\sessions.py", line 622, in send r = adapter.send(request, **kwargs) File "C:\Users\DEEP\AppData\Local\Programs\Python\Python35-32\lib\site-packages\requests\adapters.py", line 495, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
I hope you can help further.
Reply
#4
Have you inspect login (dev-tools Chrome or FireFox),to see what happens when login?
The Form Data most be correct,and you may need a some Header data.
json.dumps(payload) is not needed Requests do serializing automatic as this is build in.

With login there are many different methods and it easy to get stuck.
Requests doc Authentication has some about basic ones,eg Digest, OAuth 1 and 2,OpenId ect...

If i want to log in into this site.
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
}

params = {
    "username": "user_name",
    "password": "xxxxxxxxx",
    "remember": "yes",
    "submit": "Login",
    "action": "do_login",
}

with requests.Session() as s:
    s.post('https://python-forum.io/member.php?action=login', headers=headers, params=params)
    # logged in! cookies get saved for future requests
    response = s.get('https://python-forum.io/index.php')
    # cookies sent automatically!
    soup = BeautifulSoup(response.content, 'lxml')
    welcome = soup.find('span', class_="welcome").text
    print(welcome)
Output:
Welcome back, snippsat. You last visited: Today, 07:29 PM Log Out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  POST requests - different requests return the same response Default_001 3 1,934 Mar-10-2022, 11:26 PM
Last Post: Default_001
  requests.post() does work Alto 1 2,003 Aug-13-2021, 07:58 AM
Last Post: ndc85430
  Error posting with requests julio2000 6 2,848 Mar-28-2020, 09:43 PM
Last Post: julio2000
  requests issue with post on dot_net api Heinrich 1 2,467 Jan-23-2020, 04:28 AM
Last Post: Larz60+
  Making several POST requests RayeEThompson507 1 2,591 Nov-25-2019, 08:50 PM
Last Post: micseydel
  requests post/get to HTML form mrdominikku 1 2,319 Nov-03-2019, 07:12 PM
Last Post: Larz60+
  error with requests rudolphyaber 9 5,048 May-20-2019, 06:19 PM
Last Post: rudolphyaber
  post data problemHi guys I have small problem in python.I'm getting this error: "PO jure98 4 3,968 Apr-22-2018, 09:33 AM
Last Post: snippsat
  An Error in Requests Module pratheep 3 11,908 Feb-06-2018, 05:17 PM
Last Post: pratheep
  How do i loop through list of data from CSV file and post requests in aspx dynamics w Prince_Bhatia 1 6,078 Nov-09-2017, 02:53 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020