0

I'm a beginner in programming, and I'm trying to deal with an API to automate my work.

I'm getting the response just fine, but I'm only interested by 2 values, being host and port.

Here is a part of my code

import requests

url = "https://proxy6.net/api/xxx"

def descr():
    return 88


querystring = {"descr":descr()}

headers = {
    'Cache-Control': "no-cache",
    'Postman-Token': "xxx"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)

How can I print only the host and port value?

Thanks a lot in advance

3
  • You mean the host your script is running on, or proxy6.net? And do you expect a port other than 443? Commented Sep 5, 2018 at 21:49
  • 1
    Can you post what the output of the request is? It is good that you have excluded the API key from the code, but without it the example can not be reproduced. Commented Sep 5, 2018 at 21:49
  • {"status":"yes","user_id":"xx","balance":"xx","currency":"xx","list_count":1,"list":{"xx":{"id":"xx","version":"x,"ip":"xx","host":"xx","port":xx","user":"xx","pass":"xx","type":"socks","country":"us","date":"2018-09-05 22:00:22","date_end":"2018-10-05 22:00:22","unixtime":1536174022,"unixtime_end":1538766022,"descr":"88","active":"1"}}} Commented Sep 5, 2018 at 21:54

2 Answers 2

2
import requests
import json

url = "https://proxy6.net/api/xxx"

def descr():
    return 88


querystring = {"descr":descr()}

headers = {
    'Cache-Control': "no-cache",
    'Postman-Token': "xxx"
    }

response = requests.request("GET", url, headers=headers, params=querystring)
response = response.json()
print(response['list']['xx']['host'])
print(response['list']['xx']['port'])

Explanations:

{"status":"yes","user_id":"xx","balance":"xx","currency":"xx","list_count":1,"list":{"xx":{"id":"xx","version":"x,"ip":"xx","host":"xx","port":xx","user":"xx","pass":"xx","type":"socks","country":"us","date":"2018-09-05 22:00:22","date_end":"2018-10-05 22:00:22","unixtime":1536174022,"unixtime_end":1538766022,"descr":"88","active":"1"}}}


response['list'] gives `{"xx":{"id":"xx","version":"x,"ip":"xx","host":"xx","port":xx","user":"xx","pass":"xx","type":"socks","country":"us","date":"2018-09-05 22:00:22","date_end":"2018-10-05 22:00:22","unixtime":1536174022,"unixtime_end":1538766022,"descr":"88","active":"1"}}`


response['list']['xx']  gives  `{"id":"xx","version":"x,"ip":"xx","host":"xx","port":xx","user":"xx","pass":"xx","type":"socks","country":"us","date":"2018-09-05 22:00:22","date_end":"2018-10-05 22:00:22","unixtime":1536174022,"unixtime_end":1538766022,"descr":"88","active":"1"}`

response['list']['xx']['host'] gives host key value
response['list']['xx']['port'] gives port key value

Do let me know if you have doubts

Sign up to request clarification or add additional context in comments.

2 Comments

Getting a KeyError with that one
Check now @Vanity
1
response = requests.request(...)
data = response.json()
for value in data['list'].values():
    host = value['host']
    port = value['port']

    print host, port  # OP uses Python 2

    break
else:  # we didn't break, which means data['list'] was empty
    raise RuntimeError('Empty list')

response2 = requests.request('POST', ...)  # you can use host and port here

5 Comments

Alright, getting this back: (u'168.235.xx.xx', u'23xxx') Will there be a way to extract those too and implement it into another post request?
I assumed you're using Python 3 (which you really should btw), in Python 2 you should omit the parentheses in the print statement and you'll get the output you want.
Using Python 2. Thanks a lot for your help. I'll now try to implement it with the second piece of code I have (trying to link 2 APIs)
Thanks. This is the piece of code I have for my other API I want to make the link with. payload = "{\n\t\"generateZeroFingerprintsData\": true,\n \"name\": \"descr\",\n Removed a whole part, but I would like here, for example, to set the name to what I defined with descr. I don't recognize the format of it tho, which kind of blocks me.
Might need help on that one also, it seems that it's a format issue. Trying to do it this way \"name\": \""descr()"\" but it says invalid syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.