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')

  

posted @ 2016-03-14 09:20  圆旭  阅读(263)  评论(0编辑  收藏  举报