Python Forum
HTTP response capturing issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP response capturing issue
#1
Hi All,

below script to download DSLAM configuration, getting "error a bytes-like object is required, not 'str' "
tried with encoding, still getting error

import os
import csv
import subprocess
import time
dslamUser = "admin"
dslamPass = "1234"
httpresponse = '200 OK'
with open('dslams.csv', 'r') as csvfile:
        x = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in x:
                dslam = list(row)
                dslamName = dslam[0].strip()
                dslamAddr = dslam[1].strip()
                print('Now Trying:{} IP:{}'.format(dslamName,dslamAddr))
                print ('Authenticating with DSLAM....')
                cmdargs = "--auth-no-challenge -t 5 --http-user=" + dslamUser + " --http-password=" + dslamPass + " http://" + dslamAddr + "/config-0_.dat -O /dev/null"
                data = ""
                while not ('200 OK') in data:
                      task = subprocess.Popen("wget " + cmdargs, shell=True, stdout=None, stderr=subprocess.PIPE)
                      time.sleep(3)
                      dummy, data = task.communicate()
                os.system("wget --auth-no-challenge -t 5 --http-user=" + dslamUser + " --http-password=" + dslamPass + " -O '" + dslamName + "_'`date +%Y%m%d%H%M`'.dat' http://" + dslamAddr + "/config-0_.dat")
Error:
Now Trying:24K_Glamore-1 IP:10.217.128.93 Authenticating with DSLAM.... Traceback (most recent call last): File "zyxelbackup.py", line 22, in <module> while not ('200 OK') in data: TypeError: a bytes-like object is required, not 'str'
Want to capture HTTP response

HTTP request sent, awaiting response... 200 OK
Reply
#2
Code is much easier to read if you use f-string (python 3.6 or newer required)
import os
import csv
import subprocess
import time


dslamUser = "admin"
dslamPass = "1234"
httpresponse = '200 OK'

with open('dslams.csv', 'r') as csvfile:
    x = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in x:
        dslam = list(row)
        dslamName = dslam[0].strip()
        dslamAddr = dslam[1].strip()
        print(f'Now Trying:{dslamName} IP:{dslamAddr}')
        print ('Authenticating with DSLAM....')
        cmdargs = f"--auth-no-challenge -t 5 --http-user={dslamUser} --http-password={dslamPass} "
            f"http://{dslamAddr}/config-0_.dat -O /dev/null"
        data = ""
        while not ('200 OK') in data:
            task = subprocess.Popen(f"wget {cmdargs}", shell=True, stdout=None, stderr=subprocess.PIPE)
            time.sleep(3)
            dummy, data = task.communicate()
        os.system(f"wget --auth-no-challenge -t 5 --http-user={dslamUser} --http-password={dslamPass} "
            f"-O '{dslamName}_'`date +%Y%m%d%H%M`'.dat' http://{dslamAddr}/config-0_.dat")
Reply
#3
rather than using wget, better to use requests.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Capturing BS4 values into DF and writing to CSV cubangt 18 1,849 Sep-05-2023, 01:57 PM
Last Post: cubangt
  capturing multiline output for number of parameters jss 3 766 Sep-01-2023, 05:42 PM
Last Post: jss
  Json filter is not capturing desired key/element mrapple2020 1 1,072 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  ModuleNotFoundError: No module named 'http.client'; 'http' is not a package abhishek81py 1 15,236 Jun-25-2020, 08:58 AM
Last Post: buran
  Capturing inputs values from internal python script limors11 11 5,089 Jun-16-2019, 05:05 PM
Last Post: DeaD_EyE
  Capturing a snapshot from the video sreeramp96 1 2,146 May-24-2019, 07:02 AM
Last Post: heiner55
  httplib to http.client conversion issue zatlas1 1 2,537 Apr-12-2019, 05:21 PM
Last Post: Larz60+
  HTTP response capturing issue miunika 1 1,995 Mar-16-2019, 01:46 PM
Last Post: Larz60+
  HTTP 400 response code eshwinsukhdeve 1 1,694 Mar-03-2019, 06:11 PM
Last Post: Larz60+
  Capturing error from sql Grego 1 2,424 Jun-29-2018, 11:17 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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