This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

TCP Communication

Contents

  1. TCP Communication
    1. Client
    2. Server
    3. Links
  2. Discussion

See also: UdpCommunication

Client

Here's simple code to send and receive data by TCP in Python:

 1 #!/usr/bin/env python
 2 
 3 import socket
 4 
 5 
 6 TCP_IP = '127.0.0.1'
 7 TCP_PORT = 5005
 8 BUFFER_SIZE = 1024
 9 MESSAGE = "Hello, World!"
 10 
 11 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 12 s.connect((TCP_IP, TCP_PORT))
 13 s.send(MESSAGE)
 14 data = s.recv(BUFFER_SIZE)
 15 s.close()
 16 
 17 print "received data:", data

Server

Here's simple code to serve TCP in Python:

 1 #!/usr/bin/env python
 2 
 3 import socket
 4 
 5 
 6 TCP_IP = '127.0.0.1'
 7 TCP_PORT = 5005
 8 BUFFER_SIZE = 20 # Normally 1024, but we want fast response
 9 
 10 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 11 s.bind((TCP_IP, TCP_PORT))
 12 s.listen(1)
 13 
 14 conn, addr = s.accept()
 15 print 'Connection address:', addr
 16 while 1:
 17  data = conn.recv(BUFFER_SIZE)
 18  if not data: break
 19  print "received data:", data
 20  conn.send(data) # echo
 21 conn.close()

Discussion

  • (none yet!)

2026年02月14日 16:13

AltStyle によって変換されたページ (->オリジナル) /