[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: choosing random dynamic port number

Fredrik Lundh

1/3/2008 10:21:00 PM

Emin.shopper Martinian.shopper wrote:

> Is there a good way to choose/assign random dynamic port numbers in python?
>
> I had in mind something like the following, but if multiple programs are
> generating random port numbers, is there a way to check if a given port
> number is already taken?
>
> def GenerateDynamicPortNumber():
> "Generate a random dynamic port number and return it."
> # port numbers between 49152 to 65535 are dynamic port numbers
> return 49152 + random.randrange(15000)

def GenerateDynamicPortNumber():
return 0

(to get the actual number, use getsockname() on the socket after you've
called "bind" on it)

</F>

6 Answers

Giampaolo Rodola'

1/4/2008 11:25:00 AM

0

On 3 Gen, 23:21, Fredrik Lundh <fred...@pythonware.com> wrote:
> Emin.shopper Martinian.shopper wrote:
> > Is there a good way to choose/assign random dynamic port numbers in python?
>
> > I had in mind something like the following, but if multiple programs are
> > generating random port numbers, is there a way to check if a given port
> > number is already taken?
>
> >     def GenerateDynamicPortNumber():
> >         "Generate a random dynamic port number and return it."
> >         # port numbers between 49152 to 65535 are dynamic port numbers
> >         return 49152 + random.randrange(15000)
>
>     def GenerateDynamicPortNumber():
>         return 0
>
> (to get the actual number, use getsockname() on the socket after you've
> called "bind" on it)
>
> </F>

By using 0 as port number value you let kernel choose a free
unprivileged random port:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.bind(('', 0))
>>> s.getsockname()
('0.0.0.0', 3070)

Grant Edwards

1/4/2008 3:40:00 PM

0

On 2008-01-04, Giampaolo Rodola' <gnewsg@gmail.com> wrote:

>>     def GenerateDynamicPortNumber():
>>         return 0
>>
>> (to get the actual number, use getsockname() on the socket after you've
>> called "bind" on it)
>>
>> </F>
>
> By using 0 as port number value you let kernel choose a free
> unprivileged random port:

The port number chosen isn't random on many OSes. If the OP
really wants a random port number, he'll have to generate it
himself.

--
Grant Edwards grante Yow! Look! A ladder!
at Maybe it leads to heaven,
visi.com or a sandwich!

raireme

1/24/2014 5:10:00 PM

0

On Wednesday, January 22, 2014 3:25:42 PM UTC+2, The Revd wrote:
> On Wed, 22 Jan 2014 11:31:43 +0100, "Heinrich"
>
> <Heinrich@Ruhrgasnet.de> wrote:
>
>
>
> >Advanced multispectral camouflage systems are to be supplied to Finland's
>
> >Defense Forces by Fibrotex Technologies of Israel.
>
> >The company announced it will provide four types of camouflage systems as
>
> >part of the long-term agreement that could be worth more than $47 million.
>
> >
>
> >The exact number of systems to be supplied was not disclosed, but the
>
> >company said "thousands" would be involved and the systems would be
>
> >delivered to Finland in the near future.
>
> >
>
> >"We feel great pride to have been awarded this strategic project, with its
>
> >leading-edge technical requirements," said Adi Blum, owner and chief
>
> >executive officer of Fibrotex. "The Finnish Army Material Command has been a
>
> >valued customer of ours since 2006, and we are delighted with this award and
>
> >committed to ensuring that this partnership will continue for years to
>
> >come."
>
> >
>
> >Fibrotex' multispectral camouflage systems protect against various sensors
>
> >in a variety of ranges, including ultraviolet, visual, near-infrared,
>
> >thermal, and radar. The systems that will be supplied to the Finland are
>
> >reversible, with each side providing a different camouflage coverage.
>
> >
>
> >The systems were developed to meet the camouflage properties and
>
> >environmental conditions of Scandinavia.
>
>
>
> That'll help them defend Finnllanndd against Russia!
>
> LOLK

Well said, Da Revd. Those Jews can make excellent weapons, not only camouflage systems but also missiles. Next time we will buy missiles from them.

Heinrich

1/24/2014 5:18:00 PM

0



"Rere" <raireme@hotmail.com> schreef in bericht
news:a6951c85-a1e8-45e1-b74d-2a977acc1985@googlegroups.com...
> On Wednesday, January 22, 2014 3:25:42 PM UTC+2, The Revd wrote:
>> On Wed, 22 Jan 2014 11:31:43 +0100, "Heinrich"
>>
>> <Heinrich@Ruhrgasnet.de> wrote:
>>
>>
>>
>> >Advanced multispectral camouflage systems are to be supplied to
>> >Finland's
>>
>> >Defense Forces by Fibrotex Technologies of Israel.
>>
>> >The company announced it will provide four types of camouflage systems
>> >as
>>
>> >part of the long-term agreement that could be worth more than $47
>> >million.
>>
>> >
>>
>> >The exact number of systems to be supplied was not disclosed, but the
>>
>> >company said "thousands" would be involved and the systems would be
>>
>> >delivered to Finland in the near future.
>>
>> >
>>
>> >"We feel great pride to have been awarded this strategic project, with
>> >its
>>
>> >leading-edge technical requirements," said Adi Blum, owner and chief
>>
>> >executive officer of Fibrotex. "The Finnish Army Material Command has
>> >been a
>>
>> >valued customer of ours since 2006, and we are delighted with this award
>> >and
>>
>> >committed to ensuring that this partnership will continue for years to
>>
>> >come."
>>
>> >
>>
>> >Fibrotex' multispectral camouflage systems protect against various
>> >sensors
>>
>> >in a variety of ranges, including ultraviolet, visual, near-infrared,
>>
>> >thermal, and radar. The systems that will be supplied to the Finland are
>>
>> >reversible, with each side providing a different camouflage coverage.
>>
>> >
>>
>> >The systems were developed to meet the camouflage properties and
>>
>> >environmental conditions of Scandinavia.
>>
>>
>>
>> That'll help them defend Finnllanndd against Russia!
>>
>> LOLK
>
> Well said, Da Revd. Those Jews can make excellent weapons, not only
> camouflage systems but also missiles. Next time we will buy missiles from
> them.

bull. care to explain why you bought 2nd hand tanks from the dutch insteat
of merkava tsanks from the jews ?

The Peeler

1/24/2014 5:43:00 PM

0

On Fri, 24 Jan 2014 18:18:20 +0100, Dumb Heini, the heavily medicated Dutch
resident Nazi troll and laughing stock of sci and scj, wrote:

>>> LOLK
>>
>> Well said, Da Revd. Those Jews can make excellent weapons, not only
>> camouflage systems but also missiles. Next time we will buy missiles from
>> them.
>
> bull. care to explain why you bought 2nd hand tanks from the dutch insteat
> of merkava tsanks from the jews ?

LOL!!! Can't you type ONE single sentence without completely messing it up,
Dutch PIG? You are so STUPID it almost hurts reading your posts! LOL


--
Dumb Heini about himself: "i was diagnozed with the syndromeof asperger, no
idea what it means but i am feelingperfectly oke"
MID: <7u8b41FjtnU1@mid.individual.net>

The Revd

1/24/2014 7:23:00 PM

0

On Fri, 24 Jan 2014 09:10:19 -0800 (PST), Rear <rear_me@hotmail.com>
wrote:

>On Wednesday, January 22, 2014 3:25:42 PM UTC+2, The Revd wrote:
>> On Wed, 22 Jan 2014 11:31:43 +0100, "Heinrich"
>>
>> <Heinrich@Ruhrgasnet.de> wrote:
>>
>>
>>
>> >Advanced multispectral camouflage systems are to be supplied to Finland's
>>
>> >Defense Forces by Fibrotex Technologies of Israel.
>>
>> >The company announced it will provide four types of camouflage systems as
>>
>> >part of the long-term agreement that could be worth more than $47 million.
>>
>> >
>>
>> >The exact number of systems to be supplied was not disclosed, but the
>>
>> >company said "thousands" would be involved and the systems would be
>>
>> >delivered to Finland in the near future.
>>
>> >
>>
>> >"We feel great pride to have been awarded this strategic project, with its
>>
>> >leading-edge technical requirements," said Adi Blum, owner and chief
>>
>> >executive officer of Fibrotex. "The Finnish Army Material Command has been a
>>
>> >valued customer of ours since 2006, and we are delighted with this award and
>>
>> >committed to ensuring that this partnership will continue for years to
>>
>> >come."
>>
>> >
>>
>> >Fibrotex' multispectral camouflage systems protect against various sensors
>>
>> >in a variety of ranges, including ultraviolet, visual, near-infrared,
>>
>> >thermal, and radar. The systems that will be supplied to the Finland are
>>
>> >reversible, with each side providing a different camouflage coverage.
>>
>> >
>>
>> >The systems were developed to meet the camouflage properties and
>>
>> >environmental conditions of Scandinavia.
>>
>>
>>
>> That'll help them defend Finnllanndd against Russia!
>>
>> LOLK
>
>Well said, Da Revd.

The next Russian invasion is imminent. Are you ready to get reared?