Python Socket Client??Server?????
????? ???????[ 2014/7/14 13:53:43 ] ????????Python ????????????
??????????????????Python?????дSocketЭ??Server??Client???????????
????1. Python Socket?????
????Socket????????"?????"????ó?????????"?????"?????緢????????????????????
???????????е????????????:stream??datagram??raw??stream??datagram?????????????TCPЭ????н?????raw??????????IPЭ?顣
????Python Socket??????????BSD???????????????????????齨??????TCP??????????????????????https://docs.python.org/2/library/socket.html
????2. Python Socket Server
??????????????
# -*- coding:utf-8 -*-
from socket import *
def SocketServer():
try:
Colon = ServerUrl.find(':')
IP = ServerUrl[0:Colon]
Port = int(ServerUrl[Colon+1:])
#????socket????
print 'Server start:%s'%ServerUrl
sockobj = socket(AF_INET?? SOCK_STREAM)
sockobj.setsockopt(SOL_SOCKET??SO_REUSEADDR?? 1)
#??IP????
sockobj.bind((IP?? Port))
#??????????5??????
sockobj.listen(5)
#???????????????????
while True:
#???client????
connection?? address = sockobj.accept( )
print 'Server connected by client:'?? address
while True:
#???Client?????????
data = connection.recv(1024)
#??????data?????????
if not data: break
#????????Client
RES='200 OK'
connection.send(RES)
print 'Receive MSG:%s'%data.strip()
print 'Send RES:%s
'%RES
#???Socket
connection.close( )
except Exception??ex:
print ex
ServerUrl = "192.168.16.15:9999"
SocketServer()
|
????????????????Socket???????????????sockobj.setsockopt(SOL_SOCKET??SO_REUSEADDR?? 1)??????????Python????????Socket Server?????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11