• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
wcg249165510
博客园    首页    新随笔    联系   管理    订阅  订阅

Python socket最简单

Code
#server.py

import socket 
s1
=socket.socket();
s1.bind((
'',65533))
s1.listen(
5)
while 1:
    sc,add
=s1.accept()
    
print 'add is :',add
    
print 'content is :',sc.recv(1024)
    sc.send(
"welcome")
    

#client.py

import socket
sc
=socket.socket()
sc.connect((
'localhost',65533))
sc.send(
'bullshitWorld')
print sc.recv(1024)
sc.close()


#example from internet

 
import socket   
sock 
= socket.socket(socket.AF_INET, socket.SOCK_STREAM)   
sock.bind((
'localhost', 8001))   
sock.listen(
5)   
while True:   
    connection,address 
= sock.accept()   
    
try:   
        connection.settimeout(
5)   
        buf 
= connection.recv(1024)   
        
if buf == '1':   
            connection.send(
'welcome to server!')   
        
else:   
            connection.send(
'please go out!')   
    
except socket.timeout:   
        
print 'time out'   
        connection.close() 
posted @ 2009-01-08 10:49  Dannis wu  阅读(199)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3