# coding=utf-8from socket import *
#hostip
myhost = ''
#埠myport = 10000
sockobj = socket(af_inet, sock_stream)
sockobj.bind((myhost, myport))
sockobj.listen(128)
while true:
connection, address = sockobj.accept()
print "col:", address
while true:
data = connection.recv(1024)
print data
if not data:
break
connection.send("echo" + data)
connection.close()
# coding=utf-8from socketserver import baserequesthandler,tcpserver
import traceback
class myrequest(baserequesthandler):
def handle(self):
while true:
try:
data=self.request.recv(1024).strip()
print data
print "receive from (%r):%r"%(self.client_address,data)
self.request.sendall(data)
except exception, e:
traceback.print_exc()
break
#判斷是否是引用
if __name__=="__main__":
host=""
port=10007
add=(host,port)
server=tcpserver(add,myrequest)
server.serve_forever()
# coding=utf-8from socketserver import threadingtcpserver, streamrequesthandler
import traceback
class mystreamrequesthandler(streamrequesthandler):
#重寫的方法
def handle(self):
while true:
try:
#去除左右空格的接收資料
data = self.request.recv(1024).strip()
print "receive from (%r):%r" % (self.client_address, data)
#傳送給客戶端
self.request.sendall(data)
except exception, e:
traceback.print_exc()
break
#判斷是否是引用
if __name__ == "__main__":
#主機位址,預設為本機位址
host = ""
port = 8888
#構建主機,埠元組
addr = (host, port)
server = threadingtcpserver(addr, mystreamrequesthandler)
server.serve_forever()
#coding=utf-8import socket
#服務端位址
host="localhost"
#埠port=11112
#構建位址元組
addr=(host,port)
#網路型別 tcp ,socket型別
s=socket.socket(socket.af_inet,socket.sock_stream)
s.connect(addr)
while true:
s.sendall("hello world")
data=s.recv(1024)
print data
if not data:
break
s.close()
python下的socket程式設計
雖說語言變成了python,但是socket的基本框架還是在的。首先,我們還是要看一下unix環境下的tcp框架 接著直接來看程式,開啟你的idle,我們先來看server端 import socket 匯入socket庫 import time 匯入time庫 host localhost 連線本...
python3 TCP協議下的socket
tcp server.py coding utf 8 import socket server socket.socket 建立socket物件 ip port 127.0.0.1 8001 ip位址和埠號 server.bind ip port 繫結ip位址和埠號 server.listen 2 ...
socket的區域網廣播
很多區域網都定義了乙個特殊的保留位址,稱為廣播位址.當資訊頭中目的位址域的內容為廣播位址時,該幀被區域網上所有計算機接收.這個過程稱為廣播.合法的廣播位址就是主機的網路位址加上最大的主機號。網路位址通過機器的ip位址與子網掩碼按位與得出,我們 以ip位址為10.78.202.175 子網掩碼為255...