Socks5 protocol Proxy Server written in Python, pythonsocks5

Source: Internet
Author: User

Socks5 protocol Proxy Server written in Python, pythonsocks5

Directly run the Code:

#!/usr/bin/python # Filename s5.py # Python Dynamic Socks5 Proxy # Usage: python s5.py 1080 # Background Run: nohup python s5.py 1080 & import socket, sys, select, SocketServer, struct, time class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): passclass Socks5Server(SocketServer.StreamRequestHandler):   def handle_tcp(self, sock, remote):     fdset = [sock, remote]     while True:       r, w, e = select.select(fdset, [], [])       if sock in r:         if remote.send(sock.recv(4096)) <= 0: break       if remote in r:         if sock.send(remote.recv(4096)) <= 0: break   def handle(self):     try:       pass # print 'from ', self.client_address nothing to do.       sock = self.connection       # 1. Version       sock.recv(262)       sock.send("\x05\x00");       # 2. Request       data = self.rfile.read(4)       mode = ord(data[1])       addrtype = ord(data[3])       if addrtype == 1:    # IPv4         addr = socket.inet_ntoa(self.rfile.read(4))       elif addrtype == 3:   # Domain name         addr = self.rfile.read(ord(sock.recv(1)[0]))       port = struct.unpack('>H', self.rfile.read(2))       reply = "\x05\x00\x00\x01"       try:         if mode == 1: # 1. Tcp connect           remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM)           remote.connect((addr, port[0]))           pass # print 'To', addr, port[0] nothing do to.         else:           reply = "\x05\x07\x00\x01" # Command not supported         local = remote.getsockname()         reply += socket.inet_aton(local[0]) + struct.pack(">H", local[1])      except socket.error:         # Connection refused         reply = '\x05\x05\x00\x01\x00\x00\x00\x00\x00\x00'       sock.send(reply)       # 3. Transfering       if reply[1] == '\x00': # Success         if mode == 1:  # 1. Tcp connect           self.handle_tcp(sock, remote)     except socket.error:       pass #print 'error' nothing to do .     except IndexError:       pass def main():   filename = sys.argv[0];   if len(sys.argv)<2:     print 'usage: ' + filename + ' port'     sys.exit()   socks_port = int(sys.argv[1]);     server = ThreadingTCPServer(('', socks_port), Socks5Server)   print 'bind port: %d' % socks_port + ' ok!'   server.serve_forever() if __name__ == '__main__':   main()


What is the meaning of the SOCKS5 Proxy? What is the port? What is the HTTP Proxy? What is its address and port?

SOCKS5 Introduction
If your machine has a valid Internet IP address, or you use your own machine
The modem on the dial-up Internet can communicate with any host on the Internet, then you
Generally, you do not need to care about the SOCKS5 protocol.
For machines in the internal network, the SOCKS5 protocol must be connected through some networks.
It is useful when machines on the external Internet access the outside.
SOCKS5 is a proxy protocol that communicates with front-end machines and servers using TCP/IP
Servers play an intermediary role to enable the front-end machines in the Intranet to access the Internet.
Or make communications more secure.
The SOCKS5 server simulates
A front-end behavior. The frontend and SOCKS5 communicate with each other through the TCP/IP protocol,
The front-end sends the request to the Real Server to the SOCKS5 server, and then the SOCKS5 Service
Forward the request to the Real Server. The SOCKS5 server sends communication requests to the Real Server.
The request packet itself is not changed. The SOCKS5 server receives the real service
After the server responds, it is also forwarded to the front-end.
Therefore, SOCKS5 is a proxy protocol for various TCP/IP-based application layer protocols.
It is almost omnipotent. Although it cannot understand the internal structure of the data forwarded by itself
It can faithfully forward the communication packet and complete the functions that the Protocol would have done.
Different from the SOCKS5 protocol, the HTTP proxy is implemented through the HTTP protocol, and the HTTP Proxy Server is soft
Understand the internal structure of the communication package, and modify and transfer the communication program during the forwarding process.
Change. Different from the HTTP Proxy protocol, SOCKS5 is actually a transport layer proxy protocol.
We can imagine that if each specific application-layer protocol needs to design a corresponding proxy protocol expression
A specific proxy server does not support so many new protocols in any way. Because
This can be said that the emergence of SOCKS5 relieves the difficulties of various specific protocols that require specialized design of proxy protocols.
.
However, none of the software that uses the TCP/IP protocol can be unconditional.
Communication through the SOCKS5 server also requires the front-end software to have the SOCKS5 interface.
Use the SOCKS5 proxy server.
A front-end that supports the SOCKS5 protocol usually has two running states:
1) Direct communication status, without using the SOCKS5 interface. At this time, the communication is for the final Server
.
2) SOCKS5 status. Use the SOCKS5 interface to send a request to the final Server
To the SOCKS5 server. When the frontend and SOCKS5 servers initialize sessions, the frontend
I told the SOCKS5 server about the IP address and port information of the final server, so SOCKS5
The server can faithfully start the communication process with the final server according to the requirements of the front-end.

The port depends on the address of your free proxy and the IP address provided by the proxy.

HyperText Transfer Protocol (Hyper Text Transfer Protocol) is a Transfer Protocol used to Transfer HyperText from a WWW server to a local browser. It makes the browser more efficient and reduces network transmission. It not only ensures that the computer transfers hypertext documents correctly and quickly, but also determines which part of the transmitted documents and which part of the content is first displayed (such as text before graphics. This is why all the web addresses you see in your browser start with http.

The IP address is also the IP address of the HTTP proxy and the port 80 provided... the remaining full text>

How to Use the SOCKS5 Proxy server?

A proxy server is used by a proxy network user to obtain network information. It is a transfer station for network information.
Simply put, I need to read a book. Then someone will read this book to me, and the one who reads this book will act as an agent!

Role of proxy
Its function is to obtain network information by proxy network users. It is a transfer station for network information. In general, when we use a web browser to directly connect to other Internet sites to obtain network information, we will directly contact the target site server, and then the target site server will send the information back. The proxy server is another server between the browser and the Web server. With this server, the browser does not directly go to the Web server to retrieve the Web page, but sends a request to the proxy server, the signal will be sent to the proxy server first. The proxy server will retrieve the information required by the browser and send it to your browser.
SOCKS5 Proxy
SOCKS Proxy: the SOCKS proxy is different from other types of proxies. It simply transmits data packets and does not care about the application protocol. It can be an HTTP request, so SOCKS proxy servers are much faster than other types of proxy servers. SOCKS proxies are divided into SOCKS4 and SOCKS5. The difference between the two is that the SOCKS4 proxy only supports the TCP protocol (that is, the transmission control protocol ), the SOCKS5 Proxy supports both TCP and UDP (user data packet Protocol), as well as various authentication mechanisms and server-side domain name resolution. SOCKS5 can be achieved by SOCK4, but SOCKS that can be achieved by SOCKS5 may not be achieved. For example, QQ, a commonly used chat tool, needs to use SOCKS5 as a proxy, because it needs to use UDP to transmit data.

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.