V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
hard2reg
V2EX  ›  Python

学习 sock 的一个问题

  •  
  •   hard2reg · 2016-07-13 02:21:46 +08:00 · 2658 次点击
    这是一个创建于 2844 天前的主题,其中的信息可能已经有所发展或是发生改变。

    正在廖雪峰的网站上学习 sock 编程,代码纯手打照抄网站上的。然后当我一遍遍打开 client.py 的时候发现 server 显示来源端口在不断增大。。。是没有释放还是什么原因?

    client.py
    
    # -*- coding: utf-8 -*-
    
    import socket
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('127.0.0.1', 9999))
    print(s.recv(1024).decode('utf-8'))
    for data in [b'A', b'B', b'C']:
    	s.send(data)
    	print(s.recv(1024).decode('utf-8'))
    s.send(b'exit')
    s.close()
    
    server.py
    
    # -*- coding: utf-8 -*-
    
    import socket, threading, time
    
    def tcplink(sock, addr):
    	print('Accept new connection from %s:%s' % addr)
    	sock.send(b'Welcome!')
    	while True:
    		data = sock.recv(1024)
    		time.sleep(1)
    		if not data or data.decode('utf-8') == 'exit':
    			break
    		sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
    	sock.close()
    	print('Connection from %s:%s closed' % addr)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('127.0.0.1', 9999))
    s.listen(5)
    print('Waiting for connection')
    while True:
    	sock, addr = s.accept()
    	t = threading.Thread(target=tcplink, args=(sock, addr))
    	t.start()
    
    Output
    
    Accept new connection from 127.0.0.1:59721
    Accept new connection from 127.0.0.1:59722
    Accept new connection from 127.0.0.1:59723
    Accept new connection from 127.0.0.1:59724
    Accept new connection from 127.0.0.1:59725
    
    6 条回复    2016-07-13 23:02:27 +08:00
    aec4d
        1
    aec4d  
       2016-07-13 07:20:08 +08:00
    client 上面你可以自己绑定端口 s.bind(('',12345))
    然而关闭后并不是马上释放的 由操作系统自行分配
    hard2reg
        2
    hard2reg  
    OP
       2016-07-13 09:03:12 +08:00
    @aec4d 懂了谢谢
    wolegequ
        3
    wolegequ  
       2016-07-13 11:11:52 +08:00
    怎么贴代码的呀,很漂亮
    hard2reg
        4
    hard2reg  
    OP
       2016-07-13 11:16:11 +08:00   ❤️ 1
    @wolegequ 用 markdown 语法
    j4fun
        5
    j4fun  
       2016-07-13 12:25:05 +08:00
    这是因为 TCP 关闭后,客户端有一个 TIME_WAIT 时间,所以即使 closed 了一段时间内内核不会选择同一个端口。 linux 可以修改内核参数 tcp_fin_timeout 来修改这个时间。你可以用 ss -s 的 CLOSED 行看到。
    hard2reg
        6
    hard2reg  
    OP
       2016-07-13 23:02:27 +08:00
    @j4fun 大神!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5443 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 07:45 · PVG 15:45 · LAX 00:45 · JFK 03:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.