为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即可.