如何使用 telnet 或原始套接字发布文件?
我正在尝试在原始套接字中发布一个文件,我阅读了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
部分标题中不应该有“内容长度”吗?
Shouldnt there be a 'Content-Length' in the part-headers?
由于您已经确定了要使用的标头,因此我建议将其放入
像这样的多行字符串:
就这样了。 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:
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.
标头的每一行都必须以 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