Python套接字代理示例,不断收到调用bind()的错误..为什么?
I'm writing an ad-hoc proxy that takes commands from a fifo file, then hosts arbitary proxy connections using python socket and select.poll.. My problem is that very frequently, when calling socket.bind.. I get either a "Bad file descriptor" error, or a "Socket operation on non-socket", and I'm not sure why? Here is a snippet of the code:
我正在编写一个特殊的代理,从fifo文件中获取命令,然后使用python套接字和select.poll托管任意代理连接。我的问题是,当调用socket.bind时我很常见..我得到一个“错误的文件描述符“错误,或”非套接字上的套接字操作“,我不知道为什么?以下是代码片段:
pull_sock=socket.socket()
push_sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
pull_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
push_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
clients={}
# start pull connection
try:
print "connecting to:", saddr, sport
pull_sock.connect((saddr,sport))
except Exception, e:
print "unable to connect:", repr(e)
self._threadsafe_remove_dport(dport)
return
# start push server connection
try:
sleep(1)
push_sock.bind(('',dport))
push_sock.listen(1)
print "host new proxy on %d to %s:%d" % (dport, saddr, sport)
print "pushfd=",push_sock.fileno(),"pullfd=",pull_sock.fileno()
# register sockets for async polling
sockpoll = select.poll()
sockpoll.register(push_sock, select.POLLIN | select.POLLOUT)
sockpoll.register(pull_sock, select.POLLIN)
It's the bind()
command that always fails, nothing else. I've tried '' (all network cards), '127.0.0.1', 'localhost'... all do the same thing.
这是始终失败的bind()命令,没有别的。我试过''(所有网卡),'127.0.0.1','localhost'...都做同样的事情。
I have also commented out the REUSEADDR
part. but that made no difference either :(
我还注释了REUSEADDR部分。但这也没有区别:(
BTW.. using Arch Linux
BTW ..使用Arch Linux
1 个解决方案
#1
0
Hmmm.... If I declare the push_sock varaible after the pull_sock.connect
bits and just before the bind, it no longer happens?! It's almost like there is a bug in Python which is getting the two sockets confused?
嗯....如果我在pull_sock.connect位之后和绑定之前声明push_sock变量,它就不再发生了?!这几乎就像Python中的一个错误让两个套接字混淆了?
Seems to work a dream now though!
现在似乎工作了一个梦想!
更多相关文章
- 是什么导致了Python分割错误?
- python调用shell命令之三慷慨法
- Linux系统监控命令详解
- Linux开发工具(gcc gdb make shell)——GDB中list命令详解
- Linux curl 命令模拟 POST/GET 请求
- Linux基础-常用命令
- Linux中iptables基础命令
- linux基础(三)----linux命令系统学习----安装和登录命令
- shell用户配置详解及bash命令参数