Python 作为客户端一方的程序,可以绑定一个端口去与客户端建立 Socket 连接吗?如果有,应该怎样绑定?求解答
17 条回复 • 2017-06-12 10:33:30 +08:00
|
1
araraloren 2017 年 6 月 9 日
只要对端有 程序监听端口,你就能连的上,那就是服务端。。
socket 在发起连接的时候可以自己绑定一个本地的端口,不知道你是不是指的这个。。 |
|
2
mhycy 2017 年 6 月 9 日
发起请求之前先 bind 一个端口,这时候这个 socket 发起的请求会通过这个端口( ADDR:PORT )进行连接
|
|
3
ray1888 OP @mhycy 但是我 bind 之后连接会出现报错,报错信息是这样的,OSError: [WinError 10048] 通常每个套接字地址(协议 /网络地址 /端口)只允许使用一次。但是明明是没占用那个端口的
|
|
4
Cooky 2017 年 6 月 9 日 via Android
换个端口或者 netstat 看看谁在用那个端口
|
|
6
ray1888 OP lport = 3000
serverport = self.conn.recv(1024) serverport = int(serverport) self.tunnel_sock = socket.socket() #此处 tunnel_sock 为主动模式下的数据通道 self.tunnel_sock.bind((laddr, lport)) self.tunnel_sock.connect((chost, serverport)) self.tunnel_sock.send(b"active mode tunnel has been started") 报错: OSError: [WinError 10048] 通常每个套接字地址(协议 /网络地址 /端口)只允许使用一次。 但是我用 windows,和 linux 的虚拟机建立 socket 是没问题的,也是这样。在 windows 自己尝试就不行,netstat 过端口并没有被占用 |
|
7
am241 2017 年 6 月 9 日 via Android
直接 connect 就行,不用 bind
|
|
8
am241 2017 年 6 月 9 日 via Android
好像 udp 才需要 bind,tcp 连接不需要
|
|
10
cute 2017 年 6 月 9 日
不需要 bind
|
|
11
araraloren 2017 年 6 月 9 日
|
|
12
ray1888 OP @araraloren 是有特殊需求,想写一个 FTP 服务器,FTP 的主动模式需要服务器绑定 20 端口然后主动与客户端进行连接
|
|
13
araraloren 2017 年 6 月 9 日
@ray1888 那你就得注意,没有其他程序使用 20 端口,你的程序有权限使用 20 端口,20 端口是一个特权端口,不是普通端口,你可能需要相应的权限才行
|
|
14
denonw 2017 年 6 月 9 日
印象中不需要 bind 的吧?
|
|
15
ray1888 OP @araraloren 在 windows 下面已经 netstat 过,20 端口并没有占用,但是就是不能这样绑定,权限已经是管理员权限
|
|
16
hsmocc 2017 年 6 月 10 日
不知道是不是需要设置 self.tunnel_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1),lz 可以试试
|
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide