import socket
def find_service_name():
protocolname = 'tcp'
for port in [80, 25]:
print "Port: %s => service naem: %s" % (port, socket.getservbyport(port,portocolname)
print "Port: %s => service name: %s" % (53, socket.getservbyport(53, 'ndp'))
find_service_name()
import socket
from binascii import hcxlify
def convert_ipv4_addrss):
for ip_addr in ['127.0.0.1', '192.168.0.1']:
packed_ip_addr = socket.inet_aton(ip_addr)
unpacked_ip_addr = socket.inet_ntoa(packed_ip_addr)
print "IP address: %s -> packed: %s, unpacked:%s" % (ip_addr, hexlify(packed_ip_addr), unpacked_ip_addr)
授时协议
import ntplib
from time import ctime
def print_time():
ntp_client = ntplib.NTPClient()
response = ntp_client.request('time.nist.gov')
print ctime(response.tx_time)
print_time()
服务端socket(六个步骤)
客户端socket(四个步骤)
Server端
import socket
import sys
host = 'localhost'
backlog = 50
def echo_server(port):
""" simple echo server"""
# Create a TCP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Enable reuse address/port
sock = socket.setsockopt(socket.SOL_SOCKET, socket.SO_RESUSEADDR,1)
#Bind the socket to the port
server_address = (host, port)
print "Starting up echo server on %s port %s" % server_address
sock.bind(server_address)
# Listen to clients, backlog argument specifies the max no. of queued connections
sock.listen(backlog)
while True:
print "Waiting to receive messing from client"
client, address = sock.accept()
data = client.recv(2048)
print "Send %s bytes back to %s" %(data, address)
if data = 'bye':
print " Data %s" % data
client.send(data)
print "Send %s byte to %s" % (data, address)
if data == 'bye':
print "echo server stop"
break
client.close()
echo_server(9999)
Client端
import socket
import sys
import argparge
host = 'localhost'
def echo_client(port):
"""A simple echo client"""
sock = socket.socket(socket.AF_IENT, socket.SOCK_STREAM)
server_address = (host, port)
print "Connecting to %s port %s" % server_address
sock.connect(server_address)
try:
message = "bye"
print "Sending: %s" % message
sock.sendall(message)
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(2048)
amount_received += len(data)
print "Recived: %s" % data
except socket.error, e:
print "Socket error: %s" % str(e)
except Exception, e:
print "Other exception: %s" % str(e)
finally:
print "Closing connection to the server"
sock.close()
echo_client(9999)
传输端
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 9999)
print "Connecting to %s port %s" % server_address
sock.connect(server_address)
with open('data/ft_test_client.txt', 'r') as fp:
sock.sendall(fp.read())
print 'sending file'
sever端
import socket
import sys
host = 'localhost'
backlog = 50
def file_server(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = (host, port)
print "Starting up file server on %s port %s" %server_address
sock.bind(server_address)
sock.listen(backlog)
while True:
print "Waiting to receive file from client"
client, address = sock.accept()
data = client.recv(2048)
if data:
print 'received file'
with open('ft_open_server.txt', 'w') as fp:
fp.write(data)
break
client.close()
print "server closed"
file_server(9999)
server端
from SocketServer import TCPServer, BaseRequestHandler
class MyBaseRequestHandler(BaseRequestHandler):
"""
从BaseRequestHander继承,并重写handle方法
"""
def handle(self):
while(self):
try:
data = self.request.recv(1024).strip()
print "receive from (%r):%r" % (self.client_address, data)
self.requset.sendall(data.upper())
if data == 'y':
print 'server exit'
break
except:
break
host = "0.0.0.0"
port = 9997
addr = (host, port)
server = TCPServer(addr, MyBaseRequestHandlerr)
print 'server is started'
server.serve_forever()
client端
import os
import socket
import threading
import SocketServer
SERVER_HOST = 'localhost'
SERVER_PORT = 0 # Tell the kernel to pick up a port dynamically
BUF_SIZE = 1024
def client(ip, port, message):
"""A client to test threading mixin server"""
# Connect to the server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
try:
sock.sendall(message)
response = sock.recv(BUF_SIZE)
print "Client recevied: %s" % response
finally:
sock.close()
class ThreadTCPRequestHandler(SocketServer.BaseRequestHandler):
"""A example of threaded TCP request handler"""
def handle(self):
data = self.request.recv(BUF_SIZE)
current_thread = threading.current_thread()
response = '%s: %s' % (current_thread.name, data)
# print "Server sending response [current_thread name: date] = %s" % response
self.request.send(response)
class ThreadTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
""" Nothing to add here, inherited everything necessary from parents
"""
pass
def main():
# Launch the server
server = ThreadTCPServer((SERVER_HOST, SERVER_PORT), ThreadTCPRequestHandler)
ip, port = server.server_address # Retrieve the port number
print ip, port, 'server'
# Start a thread with the server -- one thread per request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread exits
server_thread.daemon = True
server_thread.start()
print 'Server loop running thread: %s' % server_thread.name
# Run clients
client(ip, port, "Hello from client 1")
client(ip, port, "Hello from client 2")
client(ip, port, "Hello from client 3")
# Server cleanup
server.shutdown()
if __name__ == '__main__':
main()
import os
import socket
import threading
import SocketServer
SERVER_HOST = 'localhost'
SERVER_PORT = 0
BUF_SIZE = 1024
ECHO_MSC = 'Hello echo server!'
class ForkingClient():
"""TCP客户端来检测FrokingProcess服务端"""
def __init__(self, ip, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((ip, port))
def run(self):
current_process_id = os.getpid() #获取进程id
print 'PID %s sending echo message to the server: %s' % (current_process_id,)
send_data_length = self.sock.send(ECHO_MSC)
print "Sent: %d characters, so far..." % send_data_length
# display server response
response = self.sock.recv(BUF_SIZE)
print "PID %s received: %s" % (current_process_id, response[5:])
def shutdown(self):
"""Cleanup the client socket"""
self.sock.close()
class ForkingServerRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
# Send the echo back to the client
data = self.request.recv(BUF_SIZE)
current_process_id = os.getpid()
response = '%s:%s' %(current_process_id, data)
print "Server sending response [current_process_id:data] = %s" % response
self.request.send(response)
return
class ForkingServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
"""Nothing to add here, inherited everything necessary from parents"""
pass
def main():
server = ForkingServer(SERVER_HOST, SERVER_PORT), ForkingServerRequestHandler
ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
print 'Server loop runing PID: %s' % os.getpid()
# Launch the client(s)
client1 = ForkingClient(ip, port)
client1.run()
client2 = ForkingClient(ip, port)
client2.run()
server.shutdown()
client1.shutdown()
client2.shutdown()
server.socket.close()
if __name__ == '__main__':
main()
UDP server
import socket
address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)
while True:
data, addr = s.recvfrom(2048)
if not data:
print "client has exist"
break
print "received:", data, "from", addr
if data:
if data == 'exit':
print 'exiting server'
break
s.close()
UDP Client
import socket
address = ('127.0.0.1', 31500)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
msg = raw_input()
if not msg:
break
s.sendto(msg, address)
if msg == 'exit':
print 'client exit'
break
s.close()
import httplib
import urlparse
import re
import urllib
DEFAULT_URL = 'http://www.qq.com'
HTTP_GOOD_CODES = [httplib.OK, httplib.FOUND, httplib.MOVED_PERMANENTLY]
def get_server_status_code(url):
"""Download just the header of a URL and return the server's status code"""
print urlparse.urlparse(url)
host, path = urlparse.urlparse(url)[1:3]
print host, path
try:
conn = httplib.HTTPConnection(host)
conn.request('GET', path)
return conn.getresponse().status
except StandardError, err:
print err
return None
print get_server_status_code(DEFAULT_URL)
import httplib
REMOTE_SERVER_HOST = 'www.baidu.com'
REMOTE_SERVER_PATH = '/'
class HTTPClient:
def __init__(self, host):
self.host = host
def fetch(self, path):
http = httplib.HTTP(self.host)
http.putrequest("GET", path)
http.puthreader("User-Agent", 'AGENT')
http.puthreader("Host", self.host)
http.puthereader("Accept", "*/*")
http.endheaders()
try:
errcode, errmsg, headers = http.getreply()
except Exception, e:
print "Client failed error code: %s message: %s headers: %s" % (errcode, errmsg, headers)
else:
print "Got homepage from %s" % self.host
file = http.getreply()
return file.read()
client = HTTPClient(REMOTE_SERVER_HOST)
print client.fetch(REMOTE_SERVER_PATH)