import asyncio
import aiohttp
async def fetch(url, session):
async with session.get(url) as resp:
json_body = await resp.json()
print(json_body)
async def main():
url = "http://httpbin.org/headers"
headers = {"Authorization": "Basic bG9naW46cGFzcw=="}
async with aiohttp.ClientSession(headers=headers) as session:
await asyncio.create_task(fetch(url, session))
if __name__ == '__main__':
asyncio.run(main())
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
...
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
hosts bool // whether any patterns contain hostnames
}
type muxEntry struct {
h Handler
pattern string
}
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if handler == nil {
panic("http: nil handler")
}
mux.Handle(pattern, HandlerFunc(handler))
}
func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock()
defer mux.mu.Unlock()
if pattern == "" {
panic("http: invalid pattern")
}
if handler == nil {
panic("http: nil handler")
}
if _, exist := mux.m[pattern]; exist {
panic("http: multiple registrations for " + pattern)
}
if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
mux.m[pattern] = muxEntry{h: handler, pattern: pattern}
if pattern[0] != '/' {
mux.hosts = true
}
}
...
if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
...
// код из пакета net/http
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
// код из пакета net/http
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
hosts bool // whether any patterns contain hostnames
}
type muxEntry struct {
h Handler
pattern string
}
// код из пакета net/http
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
... регистрировать хэндлеры вне пакета, чтобы они потом вызывались?
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
type ServeMux struct {
mu sync.RWMutex
m map[string]muxEntry
hosts bool // whether any patterns contain hostnames
}
type muxEntry struct {
h Handler
pattern string
}
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
if handler == nil {
panic("http: nil handler")
}
mux.Handle(pattern, HandlerFunc(handler))
}
func (mux *ServeMux) Handle(pattern string, handler Handler) {
mux.mu.Lock()
defer mux.mu.Unlock()
if pattern == "" {
panic("http: invalid pattern")
}
if handler == nil {
panic("http: nil handler")
}
if _, exist := mux.m[pattern]; exist {
panic("http: multiple registrations for " + pattern)
}
if mux.m == nil {
mux.m = make(map[string]muxEntry)
}
mux.m[pattern] = muxEntry{h: handler, pattern: pattern}
if pattern[0] != '/' {
mux.hosts = true
}
}
func handler(conn net.Conn) {
buf := make([]byte, 1024)
b := bufio.NewReader(conn)
for {
line, err := b.ReadBytes('\n')
if err != nil { // for example EOF
break
}
buf = append(buf, line...)
}
fmt.Print(string(buf))
}
defer conn.Close()
соединение явно закрывается: conn.Close()Может я чего-то не понимаю, но я не увидел условия выхода из самого первого цикла:
for {
conn, err := net.Dial("tcp", "адрес сервера")
...
defer conn.Close()
func handleRequest(conn net.Conn) {
b := bufio.NewReader(conn)
for {
line, err := b.ReadBytes('\n')
if err != nil { // for example EOF
break
}
fmt.Print(string(line))
conn.Write(line)
}
}
func handleRequest(conn net.Conn) {
message, _ := bufio.NewReader(conn).ReadString('\n')
fmt.Print("Message received:", string(message))
conn.Write([]byte("Message received."))
conn.Close()
}