Set port in requests

前端 未结 3 405
栀梦
栀梦 2021-02-07 04:51

I\'m attempting to make use of cgminer\'s API using Python. I\'m particularly interested in utilizing the requests library.

I understand how to

相关标签:
3条回答
  • 2021-02-07 05:32

    As someone who has learned some of the common pitfalls of python networking the hard way, I'm adding this answer to emphasize an important-but-easy-to-mess-up point about the 1st arg of requests.get():

    localhost is an alias which your computer resolves to 127.0.0.1, the IP address of its own loopback adapter. foo.com is also an alias, just one that gets resolved further away from the host.

    requests.get('foo.com:4028')                #<--fails
    requests.get('http://foo.com:4028')         #<--works usually  
    

    & for loopbacks:

    requests.get('http://127.0.0.1:4028')       #<--works
    requests.get('http://localhost:4028')       #<--works
    

    this one requires import socket & gives you the local ip of your host (aka, your address within your own LAN); it goes a little farther out from the host than just calling localhost, but not all the way out to the open-internet:

    requests.get('http://{}:4028'.format(socket.gethostbyname(socket.gethostname())))  #<--works
    
    0 讨论(0)
  • 2021-02-07 05:36

    Request is an HTTP library.

    You can specify the port in the URL http://example.com:4028/....

    But, from what I can read in a hurry here cgminer provides a RPC API (or JSON RPC?) not an HTTP interface.

    0 讨论(0)
  • 2021-02-07 05:39

    You can specify the port for the request with a colon just as you would in a browser, such as r = requests.get('http://localhost:4028'). This will block until a response is received, or until the request times out, so you don't need to worry about awaiting a response.

    You can send JSON data as a POST request using the requests.post method with the data parameter, such as

    import json, requests
    payload = {'command': 'summary'}
    r = requests.post('http://localhost:4028', data=json.dumps(payload))
    

    Accessing the response is then possible with r.text or r.json().

    Note that requests is an HTTP library - if it's not HTTP that you want then I don't believe it's possible to use requests.

    0 讨论(0)
提交回复
热议问题