python socket 简单例子

myserver.py:

import socket

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 8889))
serversocket.listen(5) # become a server socket, maximum 5 connections

print("Listening...")

while True:
connection, address = serversocket.accept()
print 'Connected with ', address
buf = connection.recv(256)
if len(buf) > 0:
print buf
break

  

myclient.py:

import socket

clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
clientsocket.send('hello')

  

原文地址:https://www.cnblogs.com/pinganzi/p/5274571.html