如何使用curl发送到端口17的请求?

huangapple go评论92阅读模式
英文:

How can I send a request to port 17 using curl?

问题

TCP端口17用于quote-of-the-day。为了一点乐趣,我使用了一段简单的Python代码来创建一个类似qotd服务器的东西:

import socket
import random

quotes = [
    "今日事,今日毕",
    "互联网上没有人会撒谎",
    "蛋糕是个谎言"
]

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 17)) # 绑定到端口17
server.listen(5)

while True:
    sock, addr = server.accept()
    quote = random.choice(quotes)
    sock.send(f"{quote}\n".encode()) # 发送报价
    sock.close()

这段代码可以在 https://gist.github.com/alphaolomi/51f27912abe699dd5db95cfbc21d1a2d#file-main-py 找到。

现在,我想使用curl发送一个请求来获取一句报价。我尝试了以下命令:curl 127.0.0.1:17,但失败并显示错误信息:

curl: (56) 连接被对等方重置

同时,我的qotd "服务器"立即报错:

Traceback (most recent call last):
File "/private/tmp/test-mysqlclient-in-devcontainer/tst/main.py", line 17, in
sock.send(f"{quote}\n".encode())
TypeError: 需要一个类似字节的对象,而不是'str'。

如何使用curl发送有效请求以获得一句报价?

英文:

TCP port 17 is used for quote-of-the-day. For a bit of fun I've stood up a what is effectively a qotd server using a simple bit of python:

import socket
import random

quotes = [
    "Never do today what you can do tomorrow",
    "Nobody lies on the internet",
    "The cake is a lie"
]

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 17)) # Bind to port 17
server.listen(5)

while True:
    sock, addr = server.accept()
    quote = random.choice(quotes)
    sock.send(f"{quote}\n")
    sock.close()

which was provided at https://gist.github.com/alphaolomi/51f27912abe699dd5db95cfbc21d1a2d#file-main-py.

I now want to send a request to it using curl. I tried: curl 127.0.0.1:17 which failed with error:

> curl: (56) Recv failure: Connection reset by peer

and my qotd "server" immediately errored with:

> Traceback (most recent call last):
File "/private/tmp/test-mysqlclient-in-devcontainer/tst/main.py", line 17, in <module>
sock.send(f"{quote}\n")
TypeError: a bytes-like object is required, not 'str'

如何使用curl发送到端口17的请求?

How can I use curl to send a valid request so I get a quote back?

答案1

得分: 1

printf &quot;&quot; | curl telnet://127.0.0.1:17

可以正常工作 如何使用curl发送到端口17的请求? (不确定为什么curl没有在stdin上发送feof而无法检测到套接字关闭,但它确实没有...所以需要printf)

不过,原始的TCP连接并不适合curl,而更适合使用 netcat

nc 127.0.0.1 17

>Traceback (most recent call last):
File "/private/tmp/test-mysqlclient-in-devcontainer/tst/main.py", line 17, in sock.send(f"{quote}\n")
TypeError: a bytes-like object is required, not 'str'

我猜测最可能的情况是这段代码是为Python2编写的,在Python2中可以通过socket.send发送字符串,而您尝试在Python3上运行该代码 - 一个Python3兼容的替代方法是

sock.send(quote.encode(&quot;utf-8&quot;))
英文:
printf &quot;&quot; | curl telnet://127.0.0.1:17

works 如何使用curl发送到端口17的请求? (not sure why curl isn't detecting the socket closing without sending feof on stdin, but it isn't... thus the printf)

raw tcp connections isn't really a job suited for curl tho, it's a job suited for netcat:

nc 127.0.0.1 17

also work

>Traceback (most recent call last):
File "/private/tmp/test-mysqlclient-in-devcontainer/tst/main.py", line 17, in sock.send(f"{quote}\n")
TypeError: a bytes-like object is required, not 'str'

my best guess is that the code is written for Python2, and in Python2 it is legal to send strings through socket.send, and you are trying to run the code on python3 - a python3 compatible replacement would be

sock.send(quote.encode(&quot;utf-8&quot;))

答案2

得分: 0

PHP cURL 主要设计用于发出 HTTP 请求,

然而,它也可用于发出其他协议的请求,例如 FTP、FTPS、SCP、SFTP、LDAP 等等。但它不支持开箱即用的每日引语(QOTD)协议。QOTD 协议是一个简单的协议,运行在端口 17 上,提供来自服务器的每日引语。

然而,您可以使用 PHP 的套接字来建立与 QOTD 服务器的连接并检索引语。

&lt;?php

// 定义 QOTD 服务器的地址和端口
$serverAddress = &#39;127.0.0.1&#39;;
$serverPort = 17;

// 建立与 QOTD 服务器的连接
$socket = stream_socket_client(&quot;tcp://$serverAddress:$serverPort&quot;, $errno, $errstr, 30);

// 检查连接是否成功
if (!$socket) {
    echo &quot;错误:$errno - $errstr&quot;;
} else {
    // 从服务器读取引语
    $quote = fgets($socket);

    // 关闭连接
    fclose($socket);

    // 输出引语
    echo &quot;每日引语:$quote&quot;;
}

英文:

PHP cURL is primarily designed for making HTTP requests,

However, it can also be used to make requests to other protocols, such as FTP, FTPS, SCP, SFTP, LDAP, and more. but it doesn't support the Quote of the Day (QOTD) protocol, out of the box. The QOTD protocol is a simple protocol that runs on port 17 and provides a quote of the day from a server.

However, you can use PHP's sockets to establish a connection to the QOTD server and retrieve the quote.

&lt;?php

// Define the QOTD server&#39;s address and port
$serverAddress = &#39;127.0.0.1&#39;;
$serverPort = 17;

// Establish a connection to the QOTD server
$socket = stream_socket_client(&quot;tcp://$serverAddress:$serverPort&quot;, $errno, $errstr, 30);

// Check if the connection was successful
if (!$socket) {
    echo &quot;Error: $errno - $errstr&quot;;
} else {
    // Read the quote from the server
    $quote = fgets($socket);

    // Close the connection
    fclose($socket);

    // Output the quote
    echo &quot;Quote of the Day: $quote&quot;;
}

huangapple
  • 本文由 发表于 2023年8月4日 05:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定