码迷,mamicode.com
首页 > 编程语言 > 详细

python socket

时间:2017-04-18 11:41:17      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:python server client

#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 = 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 = s.recv(1024)

s.close()

print ‘Received‘,repr(data)



python socket

标签:python server client

原文地址:http://4764055.blog.51cto.com/4754055/1916938

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!