johnmahugu

python tcp socket listener debian

Jun 3rd, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. import socket
  2. import sys
  3.  
  4. # Create a TCP/IP socket
  5. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6.  
  7. # Connect the socket to the port where the server is listening
  8. server_address = ('localhost', 8764)
  9. sock.connect(server_address)
  10.  
  11. # Listen for incoming connections
  12. sock.listen(1)
  13.  
  14. while True:
  15.     # Wait for a connection
  16.     print 'waiting for a connection'
  17.     connection, client_address = sock.accept()
  18.  
  19.     try:
  20.         print 'connection from', client_address
  21.  
  22.         while True:
  23.             data = connection.recv(8)
  24.             print 'received: %s' % data
  25.  
  26.     except:
  27.         print 'error'
Advertisement
Add Comment
Please, Sign In to add comment