Python设置Socket代理及实现远程摄像头控制的例子

为python设置socket代理
首先,你得下载SocksiPy这个.解压出来之后里面会有一个socks.py文件.然后你可以把这个文件复制到python安装目录里面的Lib\site-packages中.或者把这个文件复制到程序所在的目录中.
然后就可以再程序中使用socket代理来编写程序了.
下面是示例代码

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",8088)
socket.socket =socks.socksocket
import urllib2
urllib2.urlopen('http://www.baidu.com').read()


通过socket通信实现远程摄像头监控
通过python来实现摄像头监控,然后通过socket通信来将数据发送到远程服务器,这样就可以实现远程监控了.大概找了下资料,果然可以,下面贴出实现过程.
这个程序包括一个服务器和一个客户端。需要的库有 VideoCapture 和 pygame,一个用来得到摄像头的视频,一个用来显示.
服务器端,主要实现监听客户端所发送到指令,如果指令是startCam,则打开摄像头,并向客户端发送数据.

from VideoCapture import Device
import ImageDraw, sys, pygame, time
from pygame.locals import *
import socket
import time
from PIL import ImageEnhance
from threading import Thread
import traceback
import threading

 
 


# 全局变量
is_sending = False
cli_address = ('', 0)

# 主机地址和端口
host = 'localhost'
port = 10218

# 初始化UDP socket
ser_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ser_socket.bind((host, port))

# 接收线程类,用于接收客户端发送的消息
class UdpReceiver(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    self.thread_stop = False
        
  def run(self):
    while not self.thread_stop:
      # 声明全局变量,接收消息后更改
      global cli_address  
      global is_sending
      try:
        message, address = ser_socket.recvfrom(2048)
      except:
        traceback.print_exc()
        continue
      print message,cli_address
      cli_address = address
      if message == 'startCam':
        print 'start camera',
        is_sending = True
        ser_socket.sendto('startRcv', cli_address)        
      if message == 'quitCam':
        is_sending = False
        print 'quit camera',

  def stop(self):
    self.thread_stop = True


if __name__=='__main__':
  res = (640,480)
  
  cam = Device()
  cam.setResolution(res[0],res[1])
 
  
  brightness = 1.0
  contrast = 1.0
  shots = 0
  
  receiveThread = UdpReceiver()
  receiveThread.setDaemon(True)      # 该选项设置后使得主线程退出后子线程同时退出
  receiveThread.start()
  

  while 1:
    if is_sending: 
      camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness)
      camshot = ImageEnhance.Contrast(camshot).enhance(contrast)
      clock = pygame.time.Clock()
      img = cam.getImage().resize((160,120))
      data = img.tostring()
      ser_socket.sendto(data, cli_address) 
      time.sleep(0.05) 
    else:
      time.sleep(1)
  receiveThread.stop()
  ser_socket.close()    

客户端:
主要功能是像服务器端发送指令,然后接受服务器所发送过来的数据并通过pygame模块来显示出来.

# -*- coding: UTF-8 -*-

import socket, time
import pygame
from pygame.locals import *
from sys import exit

# 服务器地址,初始化socket
ser_address = ('localhost', 10218)
cli_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# 设置超时
cli_socket.settimeout(5)

# 向服务器发送消息,并判断接收时是否超时,若超时则重发
while 1:
  cli_socket.sendto('startCam', ser_address)
  try:
    message, address = cli_socket.recvfrom(2048)
    if message == 'startRcv':
      print message
      break
  except socket.timeout:
    continue

cli_socket.recvfrom(65536)

# 初始化视频窗口
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Web Camera')
pygame.display.flip()

# 设置时间,可以用来控制帧率
clock = pygame.time.Clock()


# 主循环,显示视频信息
while 1:
  try:
    data, address = cli_socket.recvfrom(65536)
  except socket.timeout:
    continue
  camshot = pygame.image.frombuffer(data, (160,120), 'RGB')
  camshot = pygame.transform.scale(camshot, (640, 480))
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      cli_socket.sendto('quitCam', ser_address)
      cli_socket.close()
      pygame.quit()
      exit()
  screen.blit(camshot, (0,0))
  pygame.display.update() 
  clock.tick(20)

客户端就是简单地向服务器发送启动消息,接收到回复后开始进入主循环开始接收视频数据并显示。
由于UDP协议不保证信息是否成功到达,因此前面设置了个重发机制,只有当客户端收到服务器的回复后,才停止发送开启消息并进入主循环.具体见注释.
使用时将localhost改成服务器IP即可.