Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
6493d788
Kaydet (Commit)
6493d788
authored
May 03, 2009
tarafından
Gregory P. Smith
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue 5379 - applies patch supplied by philipp hagemeister to fix
many problems with the ancient mcast.py demo code.
üst
62e9a653
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
77 deletions
+64
-77
README
Demo/sockets/README
+1
-8
mcast.py
Demo/sockets/mcast.py
+63
-69
No files found.
Demo/sockets/README
Dosyayı görüntüle @
6493d788
...
@@ -5,17 +5,10 @@ echosvr.py About the simplest TCP server possible.
...
@@ -5,17 +5,10 @@ echosvr.py About the simplest TCP server possible.
finger.py Client for the 'finger' protocol.
finger.py Client for the 'finger' protocol.
ftp.py A very simple ftp client.
ftp.py A very simple ftp client.
gopher.py A simple gopher client.
gopher.py A simple gopher client.
mcast.py IPv4/v6 multicast example
radio.py Receive time broadcasts from broadcast.py.
radio.py Receive time broadcasts from broadcast.py.
telnet.py Client for the 'telnet' protocol.
telnet.py Client for the 'telnet' protocol.
throughput.py Client and server to measure TCP throughput.
throughput.py Client and server to measure TCP throughput.
unixclient.py Unix socket example, client side
unixclient.py Unix socket example, client side
unixserver.py Unix socket example, server side
unixserver.py Unix socket example, server side
udpecho.py Client and server for the UDP echo protocol.
udpecho.py Client and server for the UDP echo protocol.
The following file is only relevant on SGI machines (or other systems
that support multicast):
mcast.py A Python translation of
/usr/people/4Dgifts/examples/network/mcast.c
(Note that IN.py is in ../../lib/sgi.)
Demo/sockets/mcast.py
Dosyayı görüntüle @
6493d788
#!/usr/bin/env python
#
# Send/receive UDP multicast packets.
# Send/receive UDP multicast packets.
# Requires that your OS kernel supports IP multicast.
# Requires that your OS kernel supports IP multicast.
# This is built-in on SGI, still optional for most other vendors.
#
#
# Usage:
# Usage:
# mcast -s (sender)
# mcast -s (sender, IPv4)
# mcast -b (sender, using broadcast instead multicast)
# mcast -s -6 (sender, IPv6)
# mcast (receivers)
# mcast (receivers, IPv4)
# mcast -6 (receivers, IPv6)
MYPORT
=
8123
MYPORT
=
8123
MYGROUP
=
'225.0.0.250'
MYGROUP_4
=
'225.0.0.250'
MYGROUP_6
=
'ff15:7079:7468:6f6e:6465:6d6f:6d63:6173'
MYTTL
=
1
# Increase to reach other networks
import
sys
import
ipaddr
import
time
import
time
import
struct
import
struct
from
socket
import
*
import
socket
import
sys
# Main program
def
main
():
def
main
():
flags
=
sys
.
argv
[
1
:]
group
=
MYGROUP_6
if
"-6"
in
sys
.
argv
[
1
:]
else
MYGROUP_4
#
if
flags
:
if
"-s"
in
sys
.
argv
[
1
:]:
sender
(
flags
[
0
])
sender
(
group
)
else
:
receiver
(
group
)
def
_sockfam
(
ip
):
"""Returns the family argument of socket.socket"""
if
ip
.
version
==
4
:
return
socket
.
AF_INET
elif
ip
.
version
==
6
:
return
socket
.
AF_INET6
else
:
else
:
receiver
()
raise
ValueError
(
'IPv'
+
ip
.
version
+
' is not supported'
)
def
sender
(
group
):
group_ip
=
ipaddr
.
IP
(
group
)
s
=
socket
.
socket
(
_sockfam
(
group_ip
),
socket
.
SOCK_DGRAM
)
# Sender subroutine (only one per local area network)
# Set Time-to-live (optional)
def
sender
(
flag
):
ttl_bin
=
struct
.
pack
(
'@i'
,
MYTTL
)
s
=
socket
(
AF_INET
,
SOCK_DGRAM
)
if
group_ip
.
version
==
4
:
if
flag
==
'-b'
:
s
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_MULTICAST_TTL
,
ttl_bin
)
s
.
setsockopt
(
SOL_SOCKET
,
SO_BROADCAST
,
1
)
mygroup
=
'<broadcast>'
else
:
else
:
mygroup
=
MYGROUP
s
.
setsockopt
(
socket
.
IPPROTO_IPV6
,
socket
.
IPV6_MULTICAST_HOPS
,
ttl_bin
)
ttl
=
struct
.
pack
(
'b'
,
1
)
# Time-to-live
s
.
setsockopt
(
IPPROTO_IP
,
IP_MULTICAST_TTL
,
ttl
)
while
True
:
while
1
:
data
=
repr
(
time
.
time
())
data
=
repr
(
time
.
time
())
## data = data + (1400 - len(data)) * '\0'
s
.
sendto
(
data
+
'
\0
'
,
(
group_ip
.
ip_ext_full
,
MYPORT
))
s
.
sendto
(
data
,
(
mygroup
,
MYPORT
))
time
.
sleep
(
1
)
time
.
sleep
(
1
)
# Receiver subroutine (as many as you like)
def
receiver
(
group
):
def
receiver
():
group_ip
=
ipaddr
.
IP
(
group
)
# Open and initialize the socket
s
=
openmcastsock
(
MYGROUP
,
MYPORT
)
#
# Loop, printing any data we receive
while
1
:
data
,
sender
=
s
.
recvfrom
(
1500
)
while
data
[
-
1
:]
==
'
\0
'
:
data
=
data
[:
-
1
]
# Strip trailing \0's
print
sender
,
':'
,
repr
(
data
)
# Open a UDP socket, bind it to a port and select a multicast group
def
openmcastsock
(
group
,
port
):
# Import modules used only here
import
string
import
struct
#
# Create a socket
# Create a socket
s
=
socket
(
AF_INET
,
SOCK_DGRAM
)
s
=
socket
.
socket
(
_sockfam
(
group_ip
),
socket
.
SOCK_DGRAM
)
#
# Allow multiple copies of this program on one machine
# Allow multiple copies of this program on one machine
# (not strictly needed)
# (not strictly needed)
s
.
setsockopt
(
SOL_SOCKET
,
SO_REUSEADDR
,
1
)
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
#
# Bind it to the port
# Bind it to the port
s
.
bind
((
''
,
port
))
s
.
bind
((
''
,
MYPORT
))
#
# Look up multicast group address in name server
# Join group
# (doesn't hurt if it is already in ddd.ddd.ddd.ddd format)
if
group_ip
.
version
==
4
:
# IPv4
group
=
gethostbyname
(
group
)
mreq
=
group_ip
.
packed
+
struct
.
pack
(
'=I'
,
socket
.
INADDR_ANY
)
#
s
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_ADD_MEMBERSHIP
,
mreq
)
# Construct binary group address
else
:
bytes
=
map
(
int
,
string
.
split
(
group
,
"."
))
mreq
=
group_ip
.
packed
+
struct
.
pack
(
'@I'
,
0
)
grpaddr
=
0
s
.
setsockopt
(
socket
.
IPPROTO_IPV6
,
socket
.
IPV6_JOIN_GROUP
,
mreq
)
for
byte
in
bytes
:
grpaddr
=
(
grpaddr
<<
8
)
|
byte
#
# Loop, printing any data we receive
# Construct struct mreq from grpaddr and ifaddr
while
True
:
ifaddr
=
INADDR_ANY
data
,
sender
=
s
.
recvfrom
(
1500
)
mreq
=
struct
.
pack
(
'll'
,
htonl
(
grpaddr
),
htonl
(
ifaddr
))
while
data
[
-
1
:]
==
'
\0
'
:
data
=
data
[:
-
1
]
# Strip trailing \0's
#
print
(
str
(
sender
)
+
' '
+
repr
(
data
))
# Add group membership
s
.
setsockopt
(
IPPROTO_IP
,
IP_ADD_MEMBERSHIP
,
mreq
)
#
if
__name__
==
'__main__'
:
return
s
main
()
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment