日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

银河计算机网络,Python脚本之socket

發布時間:2025/4/16 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 银河计算机网络,Python脚本之socket 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

##############################################socket單線程

server端:

#!/usr/bin/env python

import socket,time,os

host=''

port=18000

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

s.bind((host,port))

s.listen(1)

while 1:

conn,addr=s.accept()

print 'connected by',addr

while 1:

data=conn.recv(8192)

cmd=os.popen(data)

cmd_smg='\033[32;1mFeedback of the cmd\033[0m\n'+cmd.read()

if not data:break

#conn.sendall(data.upper())

conn.sendall(cmd_smg)

print 'connected by',addr

#print 'receievd cmd:'%data.upper()

print cmd_smg

conn.close()

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

客戶端:

#!/usr/bin/env python

#_*_ coding:utf-8 _*_

import socket,time,random,tab

host='192.168.1.10'

port=18000

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

s.connect((host,port))

while 1:

#smg=random.randint(1,100)

cmd=raw_input('input cmd:')

#s.sendall('hello my name is xiaohong:%s'%smg)

s.sendall(cmd)

data=s.recv(8192)

print "Received from server:",data

time.sleep(1.2)

s.close()

###################################################socket server多線程

server端

#!/usr/bin/env python

import SocketServer

class MyTCPHandle(SocketServer.BaseRequestHandler):

def handle(self):

while 1:

self.data=self.request.recv(8192)

if not self.data:

print 'connected break'

break

print 'Connected from:',self.client_address

print self.data

self.request.sendall(self.data.upper())

host,port='',9999

server=SocketServer.ThreadingTCPServer((host,port),MyTCPHandle)

server.serve_forever()

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

客戶端:

#!/usr/bin/env python

#_*_ coding:utf-8 _*_

import socket,time,random,tab

host='192.168.1.10'

port=9999

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

s.connect((host,port))

while 1:

#smg=random.randint(1,100)

cmd=raw_input('input cmd:')

cmd=cmd.strip()

if len(cmd)==0:continue

#s.sendall('hello my name is xiaohong:%s'%smg)

s.sendall(cmd)

data=s.recv(8192)

print "Received from server:",data

#time.sleep(1.2)

s.close()

########################################################################

總結

以上是生活随笔為你收集整理的银河计算机网络,Python脚本之socket的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。