Python 持久套接字连接
我是 python 新手:) 我想创建持久套接字。我尝试使用文件描述符来做到这一点。我尝试的是:
打开套接字套接字连接
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
获取文件描述符编号
fd = s.fileno()
将文件描述符作为 I/O os.open(fd) 打开
但我得到 OSError: [Errno 9] Bad file detector
我说我是 python 新手,也许我的实现是错误的。但我尝试了一个更简单的示例 os.close(s.fileno())
,并且得到了相同的错误 OSError: [Errno 9] Bad file detector
我发现了一个编写的示例在红宝石中,我测试了它,它有效。 如何在 Unix 上创建持久网络套接字Ruby?
任何人都可以帮我把这个写成python吗,我想要实现的是: socket_start.py google.com (将打印 fd 编号) 睡10 socket_write.py fd_number '东西' 睡10 socket_read.py fd_number 1024
我希望你明白我想做什么。谢谢指教!
在您的回复后,我尝试了下一个代码:
1
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('google.com', 80))
s.send('GET /\n')
print s.fileno()
2
#!/usr/bin/python
import os
import sys
fd = int(sys.argv[1])
os.read(fd, 1024)
错误是 OSError: [Errno 9] Bad file detector
我确定问题是什么(我是 php 开发人员)。我认为与 php 一样,python 在关闭脚本后会删除垃圾。如何在 python 中解决这个问题?
I'm new to python :) I would like to create persistent socket. I tried to do this using file descriptors. What I tried is:
Open a socket socket connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Get it's file descriptor number
fd = s.fileno()
Open the file descriptor as I/O
os.open(fd)
But I get OSError: [Errno 9] Bad file descriptor
I said I'm new to python and maybe I'm wrong with the implementation. But I tried a simpler example os.close(s.fileno())
and I get the same error OSError: [Errno 9] Bad file descriptor
I found an example written in ruby and I tested it, it works. How do I make persistent network sockets on Unix in Ruby?
Can any one write this into python for me, what I want to achieve is:
socket_start.py google.com (thie will print the fd number)
sleep 10
socket_write.py fd_number 'something'
sleep 10
socket_read.py fd_number 1024
I hope you understand what I want to do. Thanks in advice!
After your responses I tried next code:
1
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('google.com', 80))
s.send('GET /\n')
print s.fileno()
2
#!/usr/bin/python
import os
import sys
fd = int(sys.argv[1])
os.read(fd, 1024)
And the error is OSError: [Errno 9] Bad file descriptor
I'm sure what the problem is (I'm a php developer). I think same as php, python deletes garbage after closing a script. How to solve this in python ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 os.fdopen() 打开文件描述符。
我实际上很惊讶你能走到这一步,因为
os.open
需要一个文件名和标志来说明打开文件的模式。例如fd = os .open('foo.txt', os.O_RONLY)
.正如我的示例所示,它返回一个文件描述符而不是接受一个文件描述符。You use os.fdopen() to open file descriptors.
I'm actually surprised that you got that far because
os.open
requires a filename and flags stating which mode to open the file in. for examplefd = os.open('foo.txt', os.O_RONLY)
. As my example indicates, it returns a file descriptor rather than accepting one.如果您想通过在命令行上接收的参数(即套接字)重新创建套接字,请使用socket.fromfd
If you want to re-create the socket by a parameter received on the commandline (i.e., the socket), use
socket.fromfd