<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: 404 error in Python script to DELETE objects from FMC using a CSV file in Network Security</title>
    <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098908#M38</link>
    <description>I didn't know that. Thanks</description>
    <pubDate>Sun, 07 Jun 2020 15:01:51 GMT</pubDate>
    <dc:creator>Maneck_UK</dc:creator>
    <dc:date>2020-06-07T15:01:51Z</dc:date>
    <item>
      <title>404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098838#M28</link>
      <description>&lt;P&gt;&lt;FONT color="#000000"&gt;I have a fully working python script that can POST objects to the FMC (6.5.0.4) API using a .csv file for the data.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;I want to use this same approach/method to DELETE objects, but after many days I have been unable to get the script to work.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Initially&amp;nbsp;I was adding the object to the script as data rather than in the URL, I fixed this using a&amp;nbsp;&lt;/FONT&gt;&lt;FONT color="#000000"&gt;single object, if I add the object into the script using:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;del_data = ['005056A6-6972-0ed3-0000-090194313992']&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;the URL works and the objects deletes.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;But this defeats the purpose of the script as I can delete a single object in simpler ways.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;I believe I now have a script that adds the host object ID to the url but it continues to fail with a url 404 error:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;&lt;STRONG&gt;Connection Error --&amp;gt;&lt;/STRONG&gt; 404 Client Error: Not Found for url: https://&lt;SPAN&gt;&amp;lt;FMC-NAME&amp;gt;&lt;/SPAN&gt;/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314504&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;I'm new to this Python scripting business and I would be grateful if anyone has any pointers.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;The relevant parts to my script are below:&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;Thanks in advance&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="lia-message-body lia-component-message-view-widget-body lia-component-body-signature-highlight-escalation lia-component-message-view-widget-body-signature-highlight-escalation"&gt;&lt;DIV class="lia-message-body-content"&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;&lt;FONT color="#000000"&gt;URL build script component&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;f = open("delid.csv")&lt;BR /&gt;objectsfile = csv.DictReader(f)&lt;/P&gt;&lt;P&gt;for object in objectsfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; del_data = {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; object["objectid"]&lt;BR /&gt;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;for object in del_data:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; del_url = server + api_path + object&lt;/P&gt;&lt;P&gt;try:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; r = requests.delete(del_url, headers=headers, verify=False)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; status_code = r.status_code&lt;BR /&gt;&amp;nbsp; &amp;nbsp; resp = r.text&lt;BR /&gt;&amp;nbsp; &amp;nbsp; log = open('delete_objects.log', 'a')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print(" Status Code: "+str(status_code))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; json_resp = json.loads(resp)&lt;BR /&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; if status_code == 201 or status_code == 202:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(" SUCCESS ")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; elif status_code == 400:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print((" Message: ")+ resp + ('\n'))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; r.raise_for_status()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print((" Message: ")+ resp + ('\n'))&lt;/P&gt;&lt;P&gt;except requests.exceptions.HTTPError as err:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; print("Connection Error --&amp;gt; "+str(err))&lt;BR /&gt;finally:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if r: r.close()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 11:00:43 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098838#M28</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T11:00:43Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098852#M29</link>
      <description>&lt;P&gt;In your code you do this:&lt;/P&gt;&lt;PRE&gt;for object in del_data:
    del_url = server + api_path + object

try:
    ... &lt;/PRE&gt;&lt;P&gt;When you need to indent your `try` to actually call the `del_url` every time:&lt;/P&gt;&lt;PRE&gt;for object in del_data:
    del_url = server + api_path + object
    try:
        ... put code here. &lt;/PRE&gt;&lt;P&gt;Also never use `object` as a variable name, it shadows the Python builtin class `object`. Name it `element` or something instead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your question, I'd recommend printing out your URL before you call `r.delete(..)` in order to see if it's formatted correctly. The 404 indicates that what ever resource you're trying to reach, it does not exist. You have most likely already deleted it in a previous run, and since your current version don't indent correctly, you've only called the API towards the last element in your CSV.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 11:54:59 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098852#M29</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T11:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098866#M30</link>
      <description>&lt;P&gt;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/923049"&gt;@JonasKs&lt;/a&gt;&amp;nbsp;Thanks for the help.&lt;/P&gt;&lt;P&gt;At the first time of reading the information, I needed an emoji for "my head just exploded"&lt;/P&gt;&lt;P&gt;I've read through it a few more times and I think I understand; I will go and try it.....&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 12:44:27 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098866#M30</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T12:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098882#M31</link>
      <description>&lt;P&gt;Hehe, I understand.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It really boils down to these steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. Print the URL that you post to. This will make it clear that this is happening to your code. I'll try to write it out for you, though:&lt;/P&gt;&lt;PRE&gt;for object in objectsfile:
    del_data = {
        object["objectid"]
    }&lt;/PRE&gt;&lt;P&gt;In this snippet, you create a&amp;nbsp;&lt;EM&gt;set&amp;nbsp;&lt;/EM&gt;of elements. &lt;EM&gt;del_data&lt;/EM&gt; will now look like this:&lt;/P&gt;&lt;PRE&gt;del_data = {'first element', 'second element', 'third element'}&lt;/PRE&gt;&lt;P&gt;You then loop over that set:&lt;/P&gt;&lt;PRE&gt;for object in del_data:
    del_url = server + api_path + object&lt;/PRE&gt;&lt;P&gt;And then you&amp;nbsp;&lt;STRONG&gt;exit&lt;/STRONG&gt; the loop. So what's happening is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# first loop
del_url = 'first_element'
# second loop
del_url = 'second_element'
# third loop
del_url = 'third_element'&lt;/PRE&gt;&lt;P&gt;Now, the next thing you do is to execute your code:&lt;/P&gt;&lt;PRE&gt;try:
    r = requests.delete(del_url, headers=headers, verify=False)&lt;/PRE&gt;&lt;P&gt;But as you can see, you're not doing this&amp;nbsp;&lt;EM&gt;inside&amp;nbsp;&lt;/EM&gt;your&amp;nbsp;&lt;EM&gt;for&lt;/EM&gt;-loop. So this only happens once. If we take a closer look at this request, the &lt;EM&gt;del_url&amp;nbsp;&lt;/EM&gt;is&amp;nbsp;&lt;EM&gt;third_element,&amp;nbsp;&lt;/EM&gt;and I&amp;nbsp;&lt;STRONG&gt;assume &lt;/STRONG&gt;that you really wanted this to happen for&amp;nbsp;every element in your set:&lt;/P&gt;&lt;PRE&gt;try:
    r = requests.delete('third_element', headers=headers, verify=False)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, what I wrote above is that you should make this:&lt;/P&gt;&lt;PRE&gt;for object in del_data:
   del_url = server + api_path + object

try:
    r = requests.delete(del_url, headers=headers, verify=False)&lt;/PRE&gt;&lt;P&gt;look like this:&lt;/P&gt;&lt;PRE&gt;for object in del_data:
    del_url = server + api_path + object

    try:&lt;BR /&gt;        print(del_url)&lt;BR /&gt;        r = requests.delete(del_url, headers=headers, verify=False)&lt;/PRE&gt;&lt;P&gt;That will ensure that for&amp;nbsp;&lt;EM&gt;every&amp;nbsp;&lt;/EM&gt;element in&amp;nbsp;your&amp;nbsp;&lt;EM&gt;del_data&amp;nbsp;&lt;/EM&gt;set, you will execute that command.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Don't call your variables&amp;nbsp;&lt;EM&gt;object&amp;nbsp;&lt;/EM&gt;, it's an illegal name to use in Python, due to the fact that it's a built in class.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 13:43:15 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098882#M31</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T13:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098884#M32</link>
      <description>&lt;P&gt;I have updated my code to reflect the suggested changes and&amp;nbsp;I can now delete one object from the csv file. Which is a big&amp;nbsp; improvement, thanks.&amp;nbsp;But only the last object in my csv deletes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, if there is an object that is already deleted&amp;nbsp;the, script ends and reports the 404 error. My attempts to update the error msg operation from 400 to 404 (elif status_code == 404:) to help get past the 404 results in an error event:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Status code: 404&lt;BR /&gt;Message: {"error":{"category":"FRAMEWORK","messages":[{"description":"No resource found"}],"severity":"ERROR"}}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when my script ends because of the object already being deleted I do get a 404 error:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Status code: 404&lt;BR /&gt;Connection error -&amp;gt; 404 Client Error: Not Found for url: &lt;A href="https://v-uxfmc01.brunel.ac.uk/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314100" target="_blank"&gt;https://v-uxfmc01.brunel.ac.uk/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314100&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When using this operation in my working POST object from csv script, the script would continue past an object that already existed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So now I'm looking for help again please....&lt;/P&gt;&lt;P&gt;Firstly in understanding why only the last object in my csv is used and how to get it to delete all the objects in the file?&lt;/P&gt;&lt;P&gt;and&lt;/P&gt;&lt;P&gt;How to continue past an object that no longer exists?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for everyone's help so far, I would not have got this far without you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Updated script&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;f = open("del-hostid.csv")&lt;BR /&gt;elementsfile = csv.DictReader(f)&lt;/P&gt;&lt;P&gt;for element in elementsfile:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; del_data = {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; element["objectid"],&lt;BR /&gt;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for element in del_data:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; del_url = server + api_path + element&lt;BR /&gt;&amp;nbsp; &amp;nbsp; try:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; r = requests.delete(del_url, headers=headers, verify=False)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; status_code = r.status_code&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; resp = r.text&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; log = open('delete_objects.log', 'a')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(" Status code: "+str(status_code))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; json_resp = json.loads(resp)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; log.write('\n=====\n')&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if status_code == 201 or status_code == 202:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print(" SUCCESS ")&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; elif status_code == 400:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print((" Message: ")+ resp + ('\n'))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; r.raise_for_status()&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print((" Message: ")+ resp + ('\n'))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; except requests.exceptions.HTTPError as err:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print("Connection error -&amp;gt; "+str(err))&lt;BR /&gt;&amp;nbsp; &amp;nbsp; finally:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if r: r.close()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;print('\nLog file "delete_objects.log" appended\n')&lt;BR /&gt;input("Press &amp;lt;Enter&amp;gt; to return to CMD prompt")&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 13:49:04 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098884#M32</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T13:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098889#M33</link>
      <description>&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/923049"&gt;@JonasKs&lt;/a&gt; Thanks, I wrote my update post without seeing this update (my post took ages to write). This is great help. I will take another look.</description>
      <pubDate>Sun, 07 Jun 2020 14:06:40 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098889#M33</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T14:06:40Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098896#M34</link>
      <description>&lt;P&gt;In order to not break the script if a resource is not found, you can simply add a &lt;EM&gt;if status_code == 404&lt;/EM&gt; to your logic, like&amp;nbsp;this:&lt;/P&gt;&lt;PRE&gt;        &amp;lt;... your code&amp;gt;
        elif status_code == 400:
            print(" Message: " + resp + '\n')
        elif status_code == 404:
            print(" Message: " + resp + '\n')
        else:
            r.raise_for_status()
            print(" Message: "+ resp + '\n')
        &amp;lt;...your code&amp;gt;&lt;/PRE&gt;&lt;P&gt;It's the&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;r.raise_for_status()&lt;/PRE&gt;&lt;P&gt;that actually raises an exception, making the program crash. (Unless you catch that exception)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 14:27:28 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098896#M34</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T14:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098899#M35</link>
      <description>I've looked over the extra help and I have cracked the deletion part. I just needed to indent the:&lt;BR /&gt;for element in del_data:&lt;BR /&gt;del_url = server + api_path + element&lt;BR /&gt;try:&lt;BR /&gt;so that it ran under:&lt;BR /&gt;for element in elementsfile:&lt;BR /&gt;del_data = {&lt;BR /&gt;element["objectid"],&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;f = open("del-hostid.csv")&lt;BR /&gt;elementsfile = csv.DictReader(f)&lt;BR /&gt;&lt;BR /&gt;for element in elementsfile:&lt;BR /&gt;del_data = {&lt;BR /&gt;element["objectid"],&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;for element in del_data:&lt;BR /&gt;del_url = server + api_path + element&lt;BR /&gt;try:&lt;BR /&gt;print(del_url)&lt;BR /&gt;r = requests.delete(del_url, headers=headers, verify=False)&lt;BR /&gt;status_code = r.status_code&lt;BR /&gt;resp = r.text&lt;BR /&gt;log = open('delete_objects.log', 'a')&lt;BR /&gt;print(" Status code: "+str(status_code))&lt;BR /&gt;json_resp = json.loads(resp)&lt;BR /&gt;log.write('\n=====\n')&lt;BR /&gt;log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))&lt;BR /&gt;&lt;BR /&gt;if status_code == 201 or status_code == 202:&lt;BR /&gt;print(" SUCCESS ")&lt;BR /&gt;elif status_code == 400:&lt;BR /&gt;print((" Message: ")+ resp + ('\n'))&lt;BR /&gt;else:&lt;BR /&gt;r.raise_for_status()&lt;BR /&gt;print((" Message: ")+ resp + ('\n'))&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;except requests.exceptions.HTTPError as err:&lt;BR /&gt;print("Connection error -&amp;gt; "+str(err))&lt;BR /&gt;finally:&lt;BR /&gt;if r: r.close()</description>
      <pubDate>Sun, 07 Jun 2020 14:35:54 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098899#M35</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T14:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098900#M36</link>
      <description>&lt;P&gt;I have one issue remaining, if anyone could help with it; this would be amazing:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there is an object that is already deleted&amp;nbsp;the, script ends and reports a 404 error. My attempts to update the error msg operation from 400 to 404 (elif status_code == 404:) to help get past the 404 results in an error event:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Status code: 404&lt;BR /&gt;Message: {"error":{"category":"FRAMEWORK","messages":[{"description":"No resource found"}],"severity":"ERROR"}}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when my script ends because of the object already being deleted I do get a 404 error:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Status code: 404&lt;BR /&gt;Connection error -&amp;gt; 404 Client Error: Not Found for url:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://v-uxfmc01.brunel.ac.uk/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314100" target="_blank" rel="nofollow noopener noreferrer"&gt;https://v-uxfmc01.brunel.ac.uk/api/fmc_config/v1/domain/e276abec-e0f2-11e3-8169-6d9ed49b625f/object/hosts/005056A6-6972-0ed3-0000-090194314100&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When using this operation in my working POST object from csv script, the script would continue past an object that already existed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone know how to fix my issue in getting past an object that no longer exists?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 14:39:53 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098900#M36</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T14:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098905#M37</link>
      <description>&lt;P&gt;By the way, if you click&amp;nbsp;&lt;EM&gt;reply&lt;/EM&gt; and then the &lt;EM&gt;insert&amp;nbsp;&lt;/EM&gt;&lt;EM&gt;code&lt;/EM&gt; button it's easier for us to read your code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://a.pomf.cat/bwblxv.png" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 14:50:46 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098905#M37</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T14:50:46Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098908#M38</link>
      <description>I didn't know that. Thanks</description>
      <pubDate>Sun, 07 Jun 2020 15:01:51 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098908#M38</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T15:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098909#M39</link>
      <description>&lt;PRE&gt;f = open("del-hostid.csv")
elementsfile = csv.DictReader(f)

for element in elementsfile:
    del_data = {
        element["objectid"],
    }

    for element in del_data:
        del_url = server + api_path + element
        try:
            print(del_url)
            r = requests.delete(del_url, headers=headers, verify=False)
            status_code = r.status_code
            resp = r.text
            log = open('delete_objects.log', 'a')
            print(" Status code: "+str(status_code))
            json_resp = json.loads(resp)
            log.write('\n=====\n')
            log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
        
            if status_code == 201 or status_code == 202:
                print(" SUCCESS ")
            elif status_code == 400:
                print((" Message: ")+ resp + ('\n'))
            else:
                r.raise_for_status()
                print((" Message: ")+ resp + ('\n'))
    
        except requests.exceptions.HTTPError as err:
            print("Connection error -&amp;gt; "+str(err))
        finally:
            if r: r.close()&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Jun 2020 15:02:50 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098909#M39</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T15:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098938#M40</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;f = open("del-hostid.csv")
elementsfile = csv.DictReader(f)

for element in elementsfile:
    del_data = {
        element["objectid"],
    }

for element in del_data:
    del_url = server + api_path + element
    try: 
        print(del_url)
        r = requests.delete(del_url, headers=headers, verify=False)
        status_code = r.status_code
        resp = r.text
        log = open('delete_objects.log', 'a')
        print(" Status code: "+str(status_code))
        json_resp = r.json()
        log.write('\n=====\n')
        log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
        if status_code == 201 or status_code == 202:
            print(" SUCCESS ")
        elif status_code == 400 or status_code == 404:
            print(" Message: " + resp + '\n')
        else:
            r.raise_for_status()
            print(" Message: " + resp + '\n')

    except requests.exceptions.HTTPError as err:&lt;BR /&gt;        # This will only happen if the `r.raise_for_status()` line is hit
        print("Connection error -&amp;gt; "+str(err))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've changed&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;elif status_code == 400:&lt;/PRE&gt;&lt;P&gt;to&lt;/P&gt;&lt;PRE&gt;elif status_code == 400 or status_code == 404:&lt;/PRE&gt;&lt;P&gt;and&lt;/P&gt;&lt;PRE&gt;json_resp = json.loads(resp)&lt;/PRE&gt;&lt;P&gt;to&lt;/P&gt;&lt;PRE&gt;json_resp = r.json()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;as well as removed your double for-loop.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 16:13:30 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098938#M40</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T16:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098972#M41</link>
      <description>&lt;P&gt;Weird, my reply got discarded I think?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll repost it:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;f = open("del-hostid.csv")
elementsfile = csv.DictReader(f)

for element in elementsfile:
    del_data = {
        element["objectid"],
    }

for element in del_data:
    del_url = server + api_path + element
    try: 
        print(del_url)
        r = requests.delete(del_url, headers=headers, verify=False)
        status_code = r.status_code
        resp = r.text
        log = open('delete_objects.log', 'a')
        print(" Status code: "+str(status_code))
        json_resp = r.json()
        log.write('\n=====\n')
        log.write(json.dumps(json_resp,sort_keys=True,indent=4, separators=(',', ': ')))
        if status_code == 201 or status_code == 202:
            print(" SUCCESS ")
        elif status_code == 400 or status_code == 404:
            print(" Message: " + resp + '\n')
        else:
            r.raise_for_status()
            print(" Message: " + resp + '\n')

    except requests.exceptions.HTTPError as err:
        print("Connection error -&amp;gt; "+str(err))&lt;/PRE&gt;&lt;P&gt;You had a double for-loop which wasn't needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Old : elif status_code == 400:
# Old :     print((" Message: ")+ resp + ('\n'))&lt;BR /&gt;&lt;BR /&gt;# New : elif status_code == 400 or status_code == 404: &lt;BR /&gt;# New :     print(" Message: " + resp + '\n')&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've changed these two things:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Old : json_resp = json.loads(resp)&lt;BR /&gt;# New : json_resp = r.json()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 16:56:39 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4098972#M41</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T16:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099003#M42</link>
      <description>&lt;P&gt;&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/923049"&gt;@JonasKs&lt;/a&gt;&amp;nbsp;Top notch help, thanks&lt;/P&gt;&lt;P&gt;I had tried this but it had not worked, it turns out the msg handling failure was due to the incorrect indentation which had stopped my script looping through the csv file.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 18:54:14 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099003#M42</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T18:54:14Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099009#M43</link>
      <description>&lt;P&gt;Aha, yes, 4 spaces per indention is the way to go. I really recommend using an editor that help you see those things, such a PyCharm or Visual studio code with the Python plugins. Glad you solved it.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 19:12:54 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099009#M43</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T19:12:54Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099011#M44</link>
      <description>&lt;a href="https://community.cisco.com/t5/user/viewprofilepage/user-id/923049"&gt;@JonasKs&lt;/a&gt;, damned annoying your earlier post disappeared, the post you wrote about how the script worked through the loops allowed me to work it out and understand it.&lt;BR /&gt;I just spent several hours re-writing all of my scripts cleanly so that I could re-test the POST objects, GET host object ID and create .csv and then the all important DELETE objects.&lt;BR /&gt;I completed testing using my script prior to your final update from Hotfix and whilst it wasn't as clean as your script I had everything working &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;In order to test the new changes I will need to recreate the all the objects again and then run the extractions, remove an object to test the error aspect. All of which I will do, however it will be tomorrow now.&lt;BR /&gt;I understand the clean up of the messaging but what does the following achieve please?&lt;BR /&gt;# Old : json_resp = json.loads(resp)&lt;BR /&gt;# New : json_resp = r.json()</description>
      <pubDate>Sun, 07 Jun 2020 19:20:20 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099011#M44</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T19:20:20Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099015#M45</link>
      <description>&lt;P&gt;First time really replying in here, might've been me messing up something.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;So you do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;resp = r.text&lt;BR /&gt;json_resp = json.loads(resp)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;What this does is that it converts the JSON &amp;nbsp;into a Python dictionary. How ever, the response object already has this built in. In other words, you can change those two lines into this&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;json_resp = r.json()&lt;/PRE&gt;&lt;P&gt;This works since `.json()` is a function that takes the response.text and converts it into a Python dictionary. So it’s doing the exact same thing, it’s just often easier to do, so I wanted to show you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; sorry if it confused you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 07 Jun 2020 19:38:32 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099015#M45</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-07T19:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099028#M46</link>
      <description>Thanks,&lt;BR /&gt;I will try that version tomorrow. I was confused at the start of this post but thanks to your help I have a significantly improved understanding :-).&lt;BR /&gt;I also had a post in my more normal reading location in the networking section of the forums, I've tried to link that back to you too, so you get the Kudos from that post also.&lt;BR /&gt;Thanks for your time and effort. It is very much appreciated.</description>
      <pubDate>Sun, 07 Jun 2020 20:21:30 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099028#M46</guid>
      <dc:creator>Maneck_UK</dc:creator>
      <dc:date>2020-06-07T20:21:30Z</dc:date>
    </item>
    <item>
      <title>Re: 404 error in Python script to DELETE objects from FMC using a CSV file</title>
      <link>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099110#M47</link>
      <description>&lt;P&gt;You’re welcome. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I appreciate the helpful votes!&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Good luck!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jun 2020 05:33:48 GMT</pubDate>
      <guid>https://community.cisco.com/t5/network-security/404-error-in-python-script-to-delete-objects-from-fmc-using-a/m-p/4099110#M47</guid>
      <dc:creator>JonasKs</dc:creator>
      <dc:date>2020-06-08T05:33:48Z</dc:date>
    </item>
  </channel>
</rss>

