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

【python】socket

时间:2016-08-24 14:23:30      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

UDP

udp_server.py

from datetime import datetime
import socket

server_address = (localhost, 6789)
max_size = 4096

print(Starting the server at, datetime.now())
print(Waiting for a client to call.)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(server_address)

data, client = server.recvfrom(max_size)

print(At, datetime.now(), client, said, data)
server.sendto(bAre you talking to me?, client)
server.close()

 

udp_client.py

import socket
from datetime import datetime

server_address = (localhost, 6789)
max_size = 4096

print(Starting the client at, datetime.now())
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto(bHey!, server_address)
data, server = client.recvfrom(max_size)
print(At, datetime.now(), server, said, data)
client.close()

 

TCP

tcp_server.py

from datetime import datetime
import socket

address = (localhost, 6789)
max_size = 1000

print(Starting the server at, datetime.now())
print(Waiting for a client to call.)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(address)
server.listen(5) # 最多和5个客户端连接,超过5个就会拒绝

client, addr = server.accept()
data = client.recv(max_size)

print(At, datetime.now(), client, said, data)
client.sendall(bAre you talking to me?)
client.close()
server.close()

 

tcp_client.py

import socket
from datetime import datetime

address = (localhost, 6789)
max_size = 1000

print(Starting the client at, datetime.now())
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(address)
client.sendall(bHey!)
data = client.recv(max_size)
print(At, datetime.now(), someone replied, data)
client.close()

 

【python】socket

标签:

原文地址:http://www.cnblogs.com/jzm17173/p/5802542.html

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