Python 在请求中设置端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25582875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:24:33  来源:igfitidea点击:

Set port in requests

pythonnetworkingpython-3.xpython-requests

提问by 2mac

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

我正在尝试cgminer使用 Python来使用的 API。我对利用requests图书馆特别感兴趣。

I understand how to do basic things in requests, but cgminerwants to be a little more specific. I'd like to shrink

我了解如何在 中做基本的事情requests,但cgminer想要更具体一点。我想缩小

import socket
import json

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 4028))

sock.send(json.dumps({'command': 'summary'}))

using requestsinstead.

使用requests代替。

How does one specify the port using that library, and how does one send such a json request and await a response to be stored in a variable?

如何使用该库指定端口,以及如何发送这样的 json 请求并等待将响应存储在变量中?

采纳答案by Sylvain Leroux

Requestis an HTTP library.

Request是一个 HTTP 库。

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

您可以在 URL http://example.com:4028/... 中指定端口。

But, from what I can read in a hurry herecgminerprovides a RPCAPI (or JSON RPC?) notan HTTPinterface.

但是,从我可以在匆忙读这里cgminer提供了一个RPCAPI(或JSON RPC?)不是HTTP接口。

回答by Kkelk

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.

您可以像在浏览器中一样使用冒号指定请求的端口,例如 r = requests.get('http://localhost:4028'). 这将阻塞直到收到响应,或者直到请求超时,因此您无需担心等待响应。

You can send JSON data as a POST request using the requests.postmethod with the dataparameter, such as

您可以使用requests.post带有data参数方法将 JSON 数据作为 POST 请求发送,例如

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

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

然后可以使用r.text或访问响应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.

请注意,请求是一个 HTTP 库 - 如果它不是您想要的 HTTP,那么我不相信可以使用请求。

回答by Rob Truxal

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():

作为以艰难的方式学习了 Python 网络的一些常见陷阱的人,我添加这个答案是为了强调关于1st arg 的requests.get()一个重要但容易搞砸的点:

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

localhost是您的计算机解析为的别名,127.0.0.1它自己的环回适配器的 IP 地址。foo.com也是一个别名,只是在远离主机的地方解析的别名。

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:

这个需要import socket&给你你主机的本地IP(也就是你自己局域网内的地址);它离主机远一点,而不仅仅是调用localhost,但不是一直到开放互联网:

requests.get('http://{}:4028'.format(socket.gethostbyname(socket.gethostname())))  #<--works