Net Python
Net Python
html
PyMOTW
Home TCP/IP Client and Server Page Contents
Blog Sockets can be configured to act as a server and listen TCP/IP Client and Server
for incoming messages, or connect to other applications Echo Server
The Book
as a client. After both ends of a TCP/IP socket are Echo Client
About connected, communication is bi-directional. Client and Server Together
Easy Client Connections
Site Index Echo Server Choosing an Address for Listening
If you find this This sample program, based on the one in the standard Navigation
information useful, library documentation, receives incoming messages and
consider picking up a Table of Contents
echos them back to the sender. It starts by creating a
copy of my book, The Previous: Addressing, Protocol Families
TCP/IP socket.
Python Standard and Socket Types
Library By Example. import socket Next: User Datagram Client and Server
import sys
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = The Python Standard
Library by Examp...
accept() returns an open connection between the Doug Hellmann
Best Price $24.79
server and client, along with the address of the client. or Buy New $33.87
The connection is actually a different socket on another
port (assigned by the kernel). Data is read from the
connection with recv() and transmitted with Privacy Information
sendall() .
try:
print >>sys.stderr, 'connection from'
1 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
while True:
data = connection.recv
print >>sys.stderr, 'received "
if data:
print >>sys.stderr
SMS Text
connection.sendall Gateway
else:
print >>sys.stderr
break Connect to 960+
MNOs with 1 API.
finally:
# Clean up the connection
98.9% Delivered
connection.close() <1s. Free Trial
Echo Client
The client program sets up its socket differently from
the way a server does. Instead of binding to a port and
listening, it uses connect() to attach the socket
directly to the remote address.
import socket
import sys
try:
# Send data
message = 'This is the message. It will be re
print >>sys.stderr, 'sending "
sock.sendall(message)
finally:
print >>sys.stderr, 'closing socket'
sock.close()
2 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
$ python ./socket_echo_server.py
$ python socket_echo_client.py
import socket
import sys
def get_constants(prefix):
"""Create a dictionary mapping socket module c
return dict( (getattr(socket,
for n in dir(socket
if n.startswith(
)
families = get_constants('AF_')
types = get_constants('SOCK_')
protocols = get_constants('IPPROTO_'
3 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
try:
# Send data
message = 'This is the message. It will be re
print >>sys.stderr, 'sending "
sock.sendall(message)
amount_received = 0
amount_expected = len(message
finally:
print >>sys.stderr, 'closing socket'
sock.close()
$ python socket_echo_client_easy.py
Family : AF_INET
Type : SOCK_STREAM
Protocol: IPPROTO_TCP
import socket
import sys
4 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
while True:
print >>sys.stderr, 'waiting for a connection'
connection, client_address =
try:
print >>sys.stderr, 'client connected:'
while True:
data = connection.recv
print >>sys.stderr, 'received "
if data:
connection.sendall
else:
break
finally:
connection.close()
import socket
import sys
try:
amount_received = 0
amount_expected = len(message
while amount_received < amount_expected
data = sock.recv(16)
amount_received += len(data
print >>sys.stderr, 'received "
finally:
sock.close()
$ host farnsworth.hellfly.net
$ netstat -an
5 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
$ hostname
homer
import socket
import sys
while True:
print >>sys.stderr, 'waiting for a connection'
6 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
connection, client_address =
try:
print >>sys.stderr, 'client connected:'
while True:
data = connection.recv
print >>sys.stderr, 'received "
if data:
connection.sendall
else:
break
finally:
connection.close()
$ netstat -an
7 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
Comments Community
Login
Drew Green
• 2 years ago
This is great - thanks
for the guide!
1 • Reply •
Share ›
Хайрый
Сархад Төлек
• 7 months ago
Very helpful. Many
thanks
• Reply •
Share ›
BenFriman
• a year ago
Thanks. Using netstat
-an solved my
problem of getting the
listening to work via a
router with port
forwarding. I looked
at how the listening
was set up for SSH,
where the "host" IP
address was 0.0.0.0 ,
copied that for my
listening and it now
works!
• Reply •
Share ›
Masood
Mansoori
• a year ago
Very helpful, Thank
8 of 9 10/06/2014 12:15 AM
TCP/IP Client and Server - Python Module of the... http://pymotw.com/2/socket/tcp.html
Booking.com - Hotel
www.booking.com/Alberghi
Il sito uf�ciale di Booking.com! Offerte ed Hotel di ogni
categoria.
© Copyright Doug Hellmann. | | Last updated on Aug 31, 2014. | Created using Sphinx. | Design based on "Leaves" by SmallPark |
9 of 9 10/06/2014 12:15 AM