如何使用 telnet 或原始套接字发布文件?

发布于 2024-11-06 07:04:46 字数 915 浏览 0 评论 0原文

我正在尝试在原始套接字中发布一个文件,我阅读了 RFC,我想我实际上测试了很多选项,但我现在陷入困境。

顺便说一句,我知道我可以使用 pycurl、httplib 等,但我真的想手动完成。

这里的请求:

POST /upload.php?foo=bar HTTP/1.0
Host: localhost
User-Agent: Mozilla/5.0
Content-Type: multipart/form-data; boundary=9afb0c26-7adf-11e0-b167-1c6f65955350

--9afb0c26-7adf-11e0-b167-1c6f65955350
Content-Disposition: form-data; name="files[]"; filename="image.png"
Content-Type: image/png

#PNG

IHD&#   )IDA##x##       D
                         [##
###b######j
5#r#`IEND#B`#
--9afb0c26-7adf-11e0-b167-1c6f65955350--

所有这些行都来自数组连接:

"\n".join(lines)

我尝试了 \n & \r\n

最后发送到 CRLF。

我这样读我的图像:

f = open(file, 'rb')
file_content = ''
while True:
    chunck = f.read(1024)
    file_content += chunck
    if len(chunck) == 0:
        break;

lines.append(file_content)

有什么想法吗?

I'm trying to post a file within a raw socket, I read the RFC, and I think I actually tested a lot of options but I'm now stuck.

By the way, I know I could use pycurl, httplib, etc., but I really want to do it manualy.

Here the request:

POST /upload.php?foo=bar HTTP/1.0
Host: localhost
User-Agent: Mozilla/5.0
Content-Type: multipart/form-data; boundary=9afb0c26-7adf-11e0-b167-1c6f65955350

--9afb0c26-7adf-11e0-b167-1c6f65955350
Content-Disposition: form-data; name="files[]"; filename="image.png"
Content-Type: image/png

#PNG

IHD&#   )IDA##x##       D
                         [##
###b######j
5#r#`IEND#B`#
--9afb0c26-7adf-11e0-b167-1c6f65955350--

All those lines are from an array joins :

"\n".join(lines)

I tried both with \n & \r\n

And I send to CRLF at the end.

I read my images like this:

f = open(file, 'rb')
file_content = ''
while True:
    chunck = f.read(1024)
    file_content += chunck
    if len(chunck) == 0:
        break;

lines.append(file_content)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

错爱 2024-11-13 07:04:46

部分标题中不应该有“内容长度”吗?

Shouldnt there be a 'Content-Length' in the part-headers?

也只是曾经 2024-11-13 07:04:46

由于您已经确定了要使用的标头,因此我建议将其放入
像这样的多行字符串:

# An infinitely clever way to make \r\n\r\n at end of header, although technically
# inferior to just going rnrn = '\r\n\r\n' tho.  Shut up...
rnrn = '\n'.join('\r\r\r')[:4]

# remember that each line in an http header must be terminated with \r\n.
# Since multiline strings already add a \n terminator at the end of each line, 
# all that is needed is \r at the end of each line.

header = """POST /upload.php?foo=bar HTTP/1.0\r
Host: localhost\r
User-Agent: Mozilla/5.0\r
Content-Type: multipart/form-data; boundary=9afb0c26-7adf-11e0-b167-1c6f65955350\r
--9afb0c26-7adf-11e0-b167-1c6f65955350\r
Content-Disposition: form-data; name="files[]"; filename="image.png"\r
Content-Type: image/png\r
#PNG\r
IHD&#   )IDA##x##       D\r
                     [##\r
###b######j\r
5#r#`IEND#B`#\r
--9afb0c26-7adf-11e0-b167-1c6f65955350--"""+rnrn

HOST = '' #your hostname here
PORT = 0 #your port here

from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT))

s.send(header)
return_data = s.recv(1024)
s.close()

print('Got back: ', return_data)

就这样了。 python 的真正禅宗是实际编码
这部分非常简单,真正的挑战是你用它编码的内容。

我自己正在用原始套接字编写一个 HTTP 程序。这将是一个 xchat 脚本
使用 babelfish.yahoo.com 翻译 IRC 上来自外国使用者的消息。

Since you already figured out the header you will use, I recommend putting it into
a multiline string like this:

# An infinitely clever way to make \r\n\r\n at end of header, although technically
# inferior to just going rnrn = '\r\n\r\n' tho.  Shut up...
rnrn = '\n'.join('\r\r\r')[:4]

# remember that each line in an http header must be terminated with \r\n.
# Since multiline strings already add a \n terminator at the end of each line, 
# all that is needed is \r at the end of each line.

header = """POST /upload.php?foo=bar HTTP/1.0\r
Host: localhost\r
User-Agent: Mozilla/5.0\r
Content-Type: multipart/form-data; boundary=9afb0c26-7adf-11e0-b167-1c6f65955350\r
--9afb0c26-7adf-11e0-b167-1c6f65955350\r
Content-Disposition: form-data; name="files[]"; filename="image.png"\r
Content-Type: image/png\r
#PNG\r
IHD&#   )IDA##x##       D\r
                     [##\r
###b######j\r
5#r#`IEND#B`#\r
--9afb0c26-7adf-11e0-b167-1c6f65955350--"""+rnrn

HOST = '' #your hostname here
PORT = 0 #your port here

from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT))

s.send(header)
return_data = s.recv(1024)
s.close()

print('Got back: ', return_data)

And that's pretty much it. The real zen of python is that the actual coding
part is really simple, the real challenge is what you're coding with it.

I am writing a HTTP program with raw sockets myself. It will be an xchat script
that uses babelfish.yahoo.com to translate messages on IRC from foreign speakers.

生寂 2024-11-13 07:04:46

标头的每一行都必须以 CRLF 终止。请参阅此处: https://www.rfc-editor.org/rfc/rfc2616 #section-5

Every line of the header must be terminated with CRLF. See here: https://www.rfc-editor.org/rfc/rfc2616#section-5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文