python简易socket

1.sever
[cc lang=’c]’
#!/usr/bin/python
# -*- coding:utf-8 -*-

if __name__ == ‘__main__’:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((‘localhost’, 8011))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf == ‘1’:
connection.send(‘welcome to eqi.cc server!’)
else:
mess = raw_input()
connection.send(mess)
except socket.timeout:
print ‘time out’
connection.close()

[/cc]

2.client
[cc lang=’c]’
#!/usr/bin/python
#-*- coding: UTF-8 -*-

if __name__ == ‘__main__’:
import socket
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((‘localhost’, 8011))
# import time
# time.sleep(2)
sock.send(‘2’)
print sock.recv(1024)
sock.close()

[/cc]
python socket - python简易socket

https://github.com/rongtian/scoket

发表回复

电子邮件地址不会被公开。 必填项已用*标注