首先是TCP socket:
Client:
1,import socket ,然后hostname, port;
2,socket构造:s = socket.socket(socket.AF_INET, socket.SOCKET_STREAM),AF_INET是指IPV4,SOCKET_STREAM是TCP socket,若要UDP则参数为SOCKET_DGRAM;
3,获取主机地址(host = s.gethostbyname(hostname)后连接主机 s.connect((host, port));
4,发送内容s.sendall(message),接受内容response = s.recv()
5,关闭socket,s.close()
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print "failed to create socket. Error code : " + str(msg[0]) + " , Error message : " + str(msg[1])
sys.exit();
print 'socket created'
host = 'www.baidu.com'
port = 80
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit(status=None)
s.connect((remote_ip , port))
print 'Scoket Connected to ' + host + ' on ip ' + remote_ip + ' on port ' + str(port)
message = "GET / HTTP/1.1\r\n\r\n"
try:
s.sendall(message)
except socket.error:
print 'send failed'
sys.exit()
print 'message send successfully'
<span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">def</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> recv_timeout(the_socket,timeout=</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">2</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">):</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#make socket non blocking
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> the_socket.setblocking(</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">0</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">)</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#total data partwise in an array
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> total_data=[];</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> data=</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(128, 0, 0); background-color: rgb(239, 239, 239);">''</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">;</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#beginning time
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> begin=time.time()</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">while</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">1</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#if you got some data, then break after timeout
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">if</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> total_data </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">and</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> time.time()-begin > timeout:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">break</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#if you got no data at all, wait a little longer, twice the timeout
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">elif</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> time.time()-begin > timeout*</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">2</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">break</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#recv something
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">try</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> data = the_socket.recv(</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">8192</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">)</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">if</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> data:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> total_data.</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">append</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">(data)</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#change the beginning time for measurement
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> begin=time.time()</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">else</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#sleep for sometime to indicate a gap
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> time.sleep(</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">0</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">.</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(255, 0, 0); background-color: rgb(239, 239, 239);">1</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">)</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">except</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">:</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">pass</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#join all parts to make final string
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">return</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(128, 0, 0); background-color: rgb(239, 239, 239);">''</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">.</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 0, 255); background-color: rgb(239, 239, 239);">join</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);">(total_data)</span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> </span><br style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);" /><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(0, 128, 0); background-color: rgb(239, 239, 239);">#get reply and print
</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; color: rgb(128, 0, 255); background-color: rgb(239, 239, 239);">print</span><span style="font-family: Consolas, 'Courier New'; line-height: 15px; background-color: rgb(239, 239, 239);"> recv_timeout(s)</span>
s.close()
Server:
1,构造socket2,绑定socket,s.bind((HOST, PORT))3,监听,s.listen(),参数是可以监听的连接数,超过之后就reject4,接受client_socket,conn , addr = s.accept(),返回一个连接和其地址(addr[0]是主机名,addr[1]是端口。
import socket
import sys
from thread import *
HOST = ''
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'socket created'
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'bind failed. Error code : ' + str(msg[0]) + ' error message: ' + str(msg[1])
sys.exit()
print 'socket bind completed'
s.listen(10)
print 'socket now listening'
def clientthread(conn):
conn.send('welcome to the server.Type something here please: \t\n')
while True:
data = conn.recv(1024)
response = 'OK...' + data
if not data:
break
conn.sendall(response)
while True:
conn , addr = s.accept()
print 'Connected with ' + addr[0] + ' : ' + str(addr[1])
start_new_thread(clientthread, (conn, ))
s.close()
UDP Socket
Client:
import socket #for sockets
import sys #for exit
# create dgram udp socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
host = 'localhost';
port = 8888;
while(1) :
msg = raw_input('Enter message to send : ')
try :
#Set the whole string
s.sendto(msg, (host, port))
# receive data from client (data, addr)
d = s.recvfrom(1024)
reply = d[0]
addr = d[1]
print 'Server reply : ' + reply
except socket.error, msg:
print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
Server:
import socket import sys HOST = '' # Symbolic name meaning all available interfaces PORT = 8888 # Arbitrary non-privileged port # Datagram (udp) socket try : s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) print 'Socket created' except socket.error, msg : print 'Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() # Bind socket to local host and port try: s.bind((HOST, PORT)) except socket.error , msg: print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] sys.exit() print 'Socket bind complete' #now keep talking with the client while 1: # receive data from client (data, addr) d = s.recvfrom(1024) data = d[0] addr = d[1] if not data: break reply = 'OK...' + data s.sendto(reply , addr) print 'Message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.strip() s.close()