Как позволить ядру выбрать номер порта в диапазоне (1024,5000) в программировании сокета TCP

Используйте модуль gzip :

import gzip
import os

in_file = "somefile.data"
in_data = open(in_file, "rb").read()
out_gz = "foo.gz"
gzf = gzip.open(out_gz, "wb")
gzf.write(in_data)
gzf.close()

# If you want to delete the original file after the gzip is done:
os.unlink(in_file)

Ваша ошибка: OSError: [Errno 2] No such file or directory' сообщает, что файл fullFilePath не существует. Если вам все еще нужно идти по этому пути, убедитесь, что файл существует в вашей системе, и вы используете абсолютный путь, а не относительный.

15
задан Jim Puls 27 May 2009 в 00:59
поделиться

2 ответа

Port numbers have a range of 0..65535 (although often 0 has special meaning). In the original BSD TCP implementation, only root can bind to ports 1..1023, and dynamically assigned ports were assigned from the range 1024..5000; the others were available for unprivileged static assignment. These days 1024..5000 is often not enough dynamic ports, and IANA has now officially designated the range 49152..65535 for dynamic port assignment. However even that is not enough dynamic ports for some busy servers, so the range is usually configurable (by an administrator). On modern Linux and Solaris systems (often used as servers), the default dynamic range now starts at 32768. Mac OS X and Windows Vista default to 49152..65535.

linux$ cat /proc/sys/net/ipv4/ip_local_port_range 
32768   61000

solaris$ /usr/sbin/ndd /dev/tcp tcp_smallest_anon_port tcp_largest_anon_port
32768

65535

macosx$ sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535

vista> netsh int ipv4 show dynamicport tcp
Protocol tcp Dynamic Port Range
---------------------------------
Start Port : 49152
Number of Ports : 16384 
32
ответ дан 1 December 2019 в 01:11
поделиться

Посмотрите на sysctl для своей платформы. Вот что я вижу на своем Mac:


nickf@goblin:~$ sysctl -a|grep port
...
net.inet.ip.portrange.hilast: 65535
net.inet.ip.portrange.hifirst: 49152
net.inet.ip.portrange.last: 65535
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.lowlast: 600
net.inet.ip.portrange.lowfirst: 1023
...

Это диапазоны, из которых ядро ​​просматривает эфемерные порты.

6
ответ дан 1 December 2019 в 01:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: