首页 > 代码库 > python socket

python socket

#Server


import socket


HOST = ‘‘

PORT = 51234

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

s.bind((HOST,PORT))

s.listen(1)


conn,addr = s.accept()


print ‘Connected by‘, addr

while True:

    data = http://www.mamicode.com/conn.recv(1024)

    print ‘received:‘,data

    if not data:break

    conn.sendall(data)

conn.close()

---------------------------------

#client 


import socket


HOST = ‘10.10.1.21‘

PORT = 51234

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#s.bind((HOST,PORT))

s.connect((HOST,PORT))


s.sendall("Hello, world!!!")

data = http://www.mamicode.com/s.recv(1024)

s.close()

print ‘Received‘,repr(data)



python socket