Example of setting Socket proxy in Python and implementing remote camera control

Source: Internet
Author: User
Tags set socket
This article describes how to set up a Socket proxy in Python and how to implement remote camera control. This is an example of the actual use of the socket module. For more information, see Set socket proxy for python
First, you need to download SocksiPy. after decompression, there will be a socks. py file. then you can copy this file to Lib \ site-packages in the python installation directory. or copy the file to the directory where the program is located.
Then you can use the socket proxy in the program to write the program.
The following is the sample code.

import socksimport socketsocks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",8088)socket.socket =socks.socksocketimport urllib2urllib2.urlopen('http://www.baidu.com').read()


Remote camera monitoring through socket communication
Camera monitoring is implemented using python, and data is sent to the remote server through socket communication, so that remote monitoring can be achieved. I found the following information, and I will post the implementation process below.
This program includes a server and a client. The required libraries include VideoCapture and pygame. One is used to get the video from the camera and the other is used to display the video.
The server monitors the commands sent from the client. If the command is startCam, open the camera and send data to the client.

From VideoCapture import Deviceimport ImageDraw, sys, pygame, timefrom pygame. locals import * import socketimport timefrom PIL import ImageEnhancefrom threading import Threadimport tracebackimport threading # global variable is_sending = Falsecli_address = ('', 0) # host address and port host = 'localhost' port = 10218 # initialize UDP socketser_socket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) ser_socket.setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1) ser_socket.bind (host, port) # receiving Thread class, used to receive messages sent by the client class UdpReceiver (threading. thread): def _ init _ (self): threading. thread. _ init _ (self) self. thread_stop = False def run (self): while not self. thread_stop: # declare the global variable. After receiving the message, change global cli_address global is_sending try: message, address = ser_socket.recvfrom (2048) handle T: traceback. print_exc () continue print message, cli_address = address if message = 'startcam ': print 'start cama', is_sending = True ser_socket.sendto ('startrcv', cli_address) if message = 'quitcam ': is_sending = False print 'Quit cama', def stop (self): self. thread_stop = Trueif _ name __= = '_ main _': res = (640,480) cam = Device () cam. setResolution (res [0], res [1]) brightness = 1.0 contrast = 1.0 shots = 0 incluethread = UdpReceiver () incluethread. setDaemon (True) # after this option is set, the sub-thread exits the receiveThread when the sub-thread exits. 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 (1, 0.05) else: time. sleep (1) implements ETHREAD. stop () ser_socket.close ()

Client:
The main function is to send commands on the server side, then accept the data sent by the server and display it through the pygame module.

#-*-Coding: UTF-8-*-import socket, timeimport pygamefrom pygame. locals import * from sys import exit # server address, initialize socketser_address = ('localhost', 10218) cli_socket = socket. socket (socket. AF_INET, socket. SOCK_DGRAM) # Set the timeout cli_socket.settimeout (5) # send a message to the server and determine whether the message has timed out. If the timeout occurs, resend the while 1: cli_socket.sendto ('startcam ', ser_address) try: message, address = cli_socket.recvfrom (2048) if message = 'startrcv ': print message break failed t socket. timeout: continuecli_socket.recvfrom (65536) # initialize the video window pygame. init () screen = pygame. display. set_mode (640,480) pygame. display. set_caption ('web Camera ') pygame. display. flip () # set the time, which can be used to control the frame rate clock = pygame. time. clock () # Main Loop, displaying video information while 1: try: data, address = cli_socket.recvfrom (65536) blocks t 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. BITs (camshot, (0, 0) pygame. display. update () clock. tick (20)

The client simply sends a startup message to the server. After receiving the response, the client enters the main loop and receives and displays the video data.
The UDP protocol does not guarantee that the message is successfully sent. Therefore, a re-sending mechanism is set up. Only when the client receives a response from the server, the sending of the message is stopped and the message is sent to the main loop. For details, see the notes.
Change localhost to the server IP address during use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.