Python Forum
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to loop data in urllib?
#1
Hello PyLovers,
I was trying to pass each line of the file as a value to the data. I am just pasting that part of the file. Please advise.

[line refers each line and that is the value that should be passed in the payload.
def interfaces():
   f = open ('interfaces', 'w')
   with open("uniquenames") as myfile:
       for line in myfile:
           print line
           payload = "{\"data\": {\"Attributes\": [\"name\", \"speed\", \"status__name\"], \"Filters\": {\"device__name\":\" "line" \" }, \"Type\": \"DevicePort\", \"page_size\": 2}}"
           headers = {"Content-Type": "application/json"}
           request = urllib2.Request(url, payload, headers)
           response = urllib2.urlopen(request)
           d = response.read()
           print d
Reply
#2
You don't do a request call in a loop,you first build payload json then send request with payload to server.
url should not come into the function from global namespace,use argument in function interfaces(url).
Use Requests,and not urllib.
Eg.
>>> import requests

>>> line = 'Super PC'
>>> json_data = {"device__name": line, "Type": "DevicePort", "page_size": 2}
>>> headers = {'Content-type': 'application/json'}
>>> r = requests.post('http://httpbin.org/post', headers=headers, json=json_data)
>>> r.status_code
200
>>> r.json()['json']['device__name']
'Super PC'
Reply
#3
But I have a file with a list of devices that I want to run each one as a argument to the "device__name": and get the data. How do I do that?
Reply
#4
If you need to send 1 and 1 request you can do that in a loop.
Server will probably complain if it get to many requests in a short time,
if it dos set a little delay between each request.
Eg:
>>> import requests
>>> devices = ['Super pc', 'Ninja pc', 'Alien pc'] 
>>> for item in devices:
...     json_data = {"device__name": item, "Type": "DevicePort", "page_size": 2}
...     r = requests.post('http://httpbin.org/post', headers=headers, json=json_data)
...     r.status_code
...     r.json()['json']['device__name']
...     
200
'Super pc'
200
'Ninja pc'
200
'Alien pc'
Reply
#5
This actually worked. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  urllib can't find "parse" rjdegraff42 6 1,974 Jul-24-2023, 05:28 PM
Last Post: deanhystad
  Help with urllib.request Brian177 2 2,839 Apr-21-2021, 01:58 PM
Last Post: Brian177
  urllib.request ericmt123 2 2,389 Dec-21-2020, 06:53 PM
Last Post: Larz60+
  urllib is not a package traceback cc26 3 5,295 Aug-28-2020, 09:34 AM
Last Post: snippsat
  urllib request error 404 Coco 2 4,369 May-11-2019, 02:47 PM
Last Post: Larz60+
  KeyError urllib DavidFernandez 4 3,537 Nov-21-2018, 08:34 PM
Last Post: DavidFernandez
  URLLIB.REQUEST Not Working hallofriends 1 6,355 Sep-18-2017, 05:00 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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